How to Display Drive Space status for all the serves in our environment

How to Display Drive Space status for all the serves in our environment

It was been a difficult task to find the disk space status for all the serves in our data center or domain unless and until you don’t a have a right monitoring tool. Many organizations have these types of utility and monitoring programs. In this article we will discuss some script to find the disk space status report with less effort and if you tune your script mentioned in this article properly you will have a professional presentable html or other format reports with all, the details.

Go to Accessories and Windows Power Shell and click the mode you like Shell or ISE

clip_image001

In shell mode

clip_image002

In ISE mode open the script in power shell and run it either by press F5 or press the Green Play Button.

clip_image003

In both case you will see the result.

Now the Script below will send the drive space status in your email which you have configured in the script. Copy the script below and same as DriveSpaceReportEmail.ps1 by using any editor like notepad or notepad ++ etc. This script is very easy to make changes according to your environment and setup. This PowerShell script is working and I have used it for many of my customer site and became a hero in span of time!!!

#________ Script start here __________#

# Author: Nidheesh Nattiala

# Continue even if there are errors

$ErrorActionPreference = "Continue";

#########################################################################################

# Items to change to make it work for you.

#

# EMAIL PROPERTIES

# – the $users that this report will be sent to.

# – near the end of the script the smtpserver, From and Subject.

# REPORT PROPERTIES

# – you can edit the report path and report name of the html file that is the report.

#########################################################################################

# Set your warning and critical thresholds

$percentGood = 60;

$percentWarning = 50;

$percentCritcal = 10;

# EMAIL PROPERTIES

# Set the recipients of the report.

$users = "nidheesh@dkeysolutions.com"

# $cc = "nidheesh@dkeysolutions.com", "nideesh@gmail.com", "nidheesh@live.com"

$cc = "diskusage@dkeysolutions.com"

#$users = "nidheesh@dkeysolutions.com" # I use this for testing by using my email address.

#$users = "nidheesh@dkeysolutions.com", "nideesh@gmail.com", "nidheesh@live.com"; # can be sent to individuals.

# REPORT PROPERTIES

# Path to the repor. This is where the report will save to!

$reportPath = "N:\\DiskspaceReport";

# Report name

$reportName = $datetime + "index.html";

# Path and Report name together

$diskReport = $reportPath + $reportName

#Set colors for table cell backgrounds

$redColor = "#FF0000"

$orangeColor = "#FBB917"

$greenColor = "#11EB35"

$whiteColor = "#FFFFFF"

# Count if any computers have low disk space. Do not send report if less than 1.

$i = 0;

# SERVER LIST PROPERTIES

# Get computer list to check disk space. This is just a plain text file with the servers listed out.

$computers = Get-Content "N:\DiskspaceReport\Servers\Servers.txt";

#$computers = Get-QADComputer-OSname *Server* |Select -ExpandProperty name

$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";

# Remove the report if it has already been run today so it does not append to the existing report

If (Test-Path $diskReport)

{

Remove-Item $diskReport

}

# Cleanup old files..

$Daysback = "-7"

$CurrentDate = Get-Date;

$DateToDelete = $CurrentDate.AddDays($Daysback);

Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;

# HTML PROPERTIES

# Create and write HTML Header of report

$titleDate = get-date -uformat "%m-%d-%Y – %A"

$header = "

<html>

<head>

<meta http-equiv=’Content-Type’ content=’text/html; charset=iso-8859-1′>

<title>Dkey Solutions – DiskSpace Report</title>

<STYLE TYPE=’text/css’>

<!–

td {

font-family: Tahoma;

font-size: 11px;

border-top: 1px solid #999999;

border-right: 1px solid #999999;

border-bottom: 1px solid #999999;

border-left: 1px solid #999999;

padding-top: 0px;

padding-right: 0px;

padding-bottom: 0px;

padding-left: 0px;

}

body {

margin-left: 5px;

margin-top: 5px;

margin-right: 0px;

margin-bottom: 10px;

table {

border: thin solid #000000;

}

–>

</style>

</head>

<body>

<table width=’100%’>

<tr bgcolor=’#CCCCCC’>

<td colspan=’7′ height=’25’ align=’center’>

<font face=’tahoma’ color=’#003399′ size=’4′><strong>DkeySolutions – DiskSpace Report for $titledate</strong></font>

</td>

</tr>

</table>

"

Add-Content $diskReport $header

# Create and write Table header for report

$tableHeader = "

<table width=’100%’><tbody>

<tr bgcolor=#CCCCCC>

<td width=’10%’ align=’center’>Server</td>

<td width=’5%’ align=’center’>Drive</td>

<td width=’15%’ align=’center’>Drive Label</td>

<td width=’10%’ align=’center’>Total Capacity(GB)</td>

<td width=’10%’ align=’center’>Used Capacity(GB)</td>

<td width=’10%’ align=’center’>Free Space(GB)</td>

<td width=’5%’ align=’center’>Freespace %</td>

</tr>

"

Add-Content $diskReport $tableHeader

# Start processing disk space reports against a list of servers

foreach($computer in $computers)

{

$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3"

$computer = $computer.toupper()

foreach($disk in $disks)

{

$deviceID = $disk.DeviceID;

$volName = $disk.VolumeName;

[float]$size = $disk.Size;

[float]$freespace = $disk.FreeSpace;

$percentFree = [Math]::Round(($freespace / $size) * 100, 2);

$sizeGB = [Math]::Round($size / 1073741824, 2);

$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);

$usedSpaceGB = $sizeGB – $freeSpaceGB;

$color = $whiteColor;

# Set background color to Green if it is Healthy

#if($percentFree -lt $percentGood)

# {

#$color = $greenColor

# Set background color to Orange if just a warning

if($percentFree -lt $percentWarning)

{

$color = $orangeColor

# Set background color to Orange if space is Critical

if($percentFree -lt $percentCritcal)

{

$color = $redColor

}

# Create table data rows

$dataRow = "

<tr>

<td width=’10%’>$computer</td>

<td width=’5%’ align=’center’>$deviceID</td>

<td width=’15%’ >$volName</td>

<td width=’10%’ align=’center’>$sizeGB</td>

<td width=’10%’ align=’center’>$usedSpaceGB</td>

<td width=’10%’ align=’center’>$freeSpaceGB</td>

<td width=’5%’ bgcolor=`’$color`’ align=’center’>$percentFree</td>

</tr>

"

Add-Content $diskReport $dataRow;

Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";

$i++

}

}

}

# Create table at end of report showing legend of colors for the critical and warning

$tableDescription = "

</table><br><table width=’20%’>

<tr bgcolor=’White’>

#<td width=’10%’ align=’center’ bgcolor=’#11EB35′>Healthy less than 75% free space</td>

<td width=’10%’ align=’center’ bgcolor=’#FBB917′>Warning less than 15% free space</td>

<td width=’10%’ align=’center’ bgcolor=’#FF0000′>Critical less than 10% free space</td>

</tr>

"

Add-Content $diskReport $tableDescription

Add-Content $diskReport "</body></html>"

# Send Notification if alert $i is greater than 0

if ($i -gt 0)

{

foreach ($user in $users)

{

Write-Host "Sending Email notification to $user"

# SMTP PROPERTIES

# SMTP Server settings that will allow the report to be emailed to you which has SMTP enabled

$smtpServer = "mail.dkeysolutions.com"

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

$msg = New-Object Net.Mail.MailMessage

$msg.To.Add($user)

$msg.CC.Add($cc)

$msg.From = "diskusage@dkeysolutions.com"

$msg.Subject = "Dkey Solutions Environment DiskSpace Report for $titledate"

$msg.IsBodyHTML = $true

$msg.Body = get-content $diskReport

$smtp.Send($msg)

$body = "Report completed."

}

}

#________________End of the Script_________________

The Email will be some this like this

clip_image005

This is another script which I like most because this has some nice Graphic bar and it has a nice view. . Copy the script below and same as DriveSpaceReportGraph.ps1 by using any editor like notepad or notepad ++ etc. change the location of the report file and other setting according to your need.

#________ Script start here __________#

#script will open web browser with current report when completed.

# Author: Nidheesh Nattiala

Param (

$computers = (Get-Content "N:\DiskspaceReport\Servers.txt")

)

$Title="Hard Drive Report to HTML"

#embed a stylesheet in the html header

$head = @"

<mce:style><!–

mce:0

–></mce:style><style _mce_bogus="1"><!–

mce:0

–></style>

<Title>$Title</Title>

<br>

"@

# EMAIL PROPERTIES

# Set the recipients of the report.

#$users = "nidheesh@dkeysolutions.com"

# $cc = "nideesh@gmail.com", "nidheesh@live.com"

#$cc = "diskusage@dkeysolutions.com"

#$users = "nidheesh@dkeysolutions.com" # I use this for testing by using my email address.

#$users = "nideesh@gmail.com", "nidheesh@live.com"; # can be sent to individuals.

#define an array for html fragments

$fragments=@()

#gets the drive data

$data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers

#group data by computername

$groups=$Data | Group-Object -Property SystemName

#this is the graph character

[string]$g=[char]9608

#create html fragments for each computer

#iterate through each group object

ForEach ($computer in $groups) {

$fragments+="<H2>$($computer.Name)</H2>"

#define a collection of drives from the group object

$Drives=$computer.group

#create an html fragment

$html=$drives | Select @{Name="Drive";Expression={$_.DeviceID}},

@{Name="SizeGB";Expression={$_.Size/1GB -as [int]}},

@{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size – $_.Freespace)/1GB) }},

@{Name="FreeGB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }},

@{Name="Usage";Expression={

$UsedPer= (($_.Size – $_.Freespace)/$_.Size)*100

$UsedGraph=$g * ($UsedPer/2)

$FreeGraph=$g* ((100-$UsedPer)/2)

#I’m using place holders for the < and > characters

"xopenFont color=Redxclose{0}xopen/FontxclosexopenFont Color=Greenxclose{1}xopen/fontxclose" -f $usedGraph,$FreeGraph

}} | ConvertTo-Html -Fragment

#replaces the tag place holders. It is a hack but it works.

$html=$html -replace "xopen","<"

$html=$html -replace "xclose",">"

#add to fragments

$Fragments+=$html

#insert a return between each computer

$fragments+="<br>"

} #foreach computer

#add a footer

$footer=("<br><I>Report run {0} by {1}\{2}<I>" -f (Get-Date -displayhint date),$env:userdomain,$env:username)

$fragments+=$footer

#writes the result to a file

$Path = "N:\DiskspaceReport\DriveSpacereport.htm"

ConvertTo-Html -head $head -body $fragments | Out-File $Path

#_______________End of the Script______________

The end result will be something like this.

clip_image007

Both of the script above is tested and is working and the reference I have taken for this script is form Microsoft script center. All what you have to do is to change the smtp server and email address according to your environment. Also you have to change the path of the files for the reports and the server list file. Bothe above script will run based on the computer names provided in the list mentioned in server.txt file. Computer name will be read by the script line by line so arrange the name according to in the text file. You can use task seclude in windows to schedule this script to run automatically in an agreed time frame like weekly or monthly etc., for that you have to create a batch file and use this batch file and run your script. Create a batch called DiskSpaceReport.cmd with the line below:-

powershell.exe “N:\DiskspaceReport\DiskSpaceReport.ps1"

Below are some screens about schedule task in Windows8, (Windows Server 2012 is also same) here you can search in settings, you can go ahead and Create Basic Task, rest of the steps is self-explanatory and easy to go…

clip_image008

clip_image010

clip_image012

clip_image014

Try this PowerShell script and customize according to your requirement and avoid low disk space issue especially on Database and File Servers.

How to Transfer and Seize FSMO role of a Domain Controller using ntdsutil utility

How to Transfer and Seize FSMO role of a Domain Controller using ntdsutil utility

There are graceful way to transfer FSMO role of a domain controller in a forest.which will be known to everybody and it is easy to do in Active Directory Users and Computers and Active Directory Domains and Trusts console,there are some screens below which will remind you the steps which all are self explanatory. This article is inspired form these M$ KB Articles:- http://support.microsoft.com/kb/255504 , http://support.microsoft.com/kb/324801 .

To find FSMO role, on command prompt type :- netdom query FSMO

Open Active Directory Users and Computers and right the domain name and select Operations Master from the context menu.

clip_image002

clip_image004

clip_image006

clip_image007

Open Active Directory Domains and Trusts and right the domain name and select Operations Master from the context menu.

clip_image008

clip_image009

For schema master, first register the file schmmgmt.dll by using this command regsvr32 schmmgmt.dll

clip_image010

Go to mmc ,add the snap in Active Directory Domains Schema and Right-click on Active Directory Domains Schema, and select Operations Master from the context menu.

clip_image012

clip_image013

The above steps are all graceful transfer of FSMO roles. For some reason like me I, had a situation in one of my client, I can’t do the graceful transfer,then I had gone through some hard way to transfer and seize the domain controller ,Let me share that with you guys. Below is the screen that I have got when I tried for the graceful move.

clip_image015

In this article we will use ntdsutil

http://technet.microsoft.com/en-us/library/cc976711.aspx

Type ntdsutil in the administrative command prompt, then on the ntdsutil prompt type roles, then FSMO maintenance prompt type connect to <your Domain Controller> then type q. Now you can transfer or seize role to your working domain Controller. Below is the screen for the command used along with ntdsutil

clip_image016

clip_image017

clip_image018

clip_image019

What are the difference between transferring a FSMO role and seizing?

Seizing is a destructive FSMO process and you should only use, if the existing server with the FSMO is no longer available. If the domain controller that is the Schema Master FSMO role holder is temporarily unavailable, DO NOT seizes the Schema Master role. If you are going to seize the Schema Master, It is better that permanently disconnect the current Schema Master from the network and also recommended to reformat the original schema master drive. Transferring of FSMO is not a destructive process and you can transfer the roles to any domain controller in the forest based on the recommendation.

clip_image020

clip_image021

All the options in this utility are self-explanatory,  I hope you will not have any hard time. Try to read some article before performing this operation.

How to establish trust relation between Windows 2008 R2 domain which is in a two different forest

How to establish trust Windows 2008 R2 domain which is in a two different forest

This article describes how to trust a Windows 2008 R2 domain which is in a two different forest. Domain trust is required when acquisition or inter domain exchange server configuration, single sign on, vitalization for multiple domain architect ( VMview and Xen DDC etc).In abroad way to understand this the function of trust in domain architect is very important nowadays. In Windows Server domain, any type trust allows users in one domain to access resources in another domain it can be either one-way or two-way.  In a one-way trust, as the name itself it is one way and unidirectional, one domain’s users access another domain’s resources, not the other way around.  In two-way trust, users in both domains may access the other domain’s resources.  Trust relation can be transitive or non-transitive.  The below links will give you insight knowledge…

There are so many considerations we have to follow while building a domain trust relationship especially on security and the type of relations.

There is a good article in TechNet, I recommend, please read before proceeding….

http://technet.microsoft.com/en-us/library/cc961481.aspx

Description about Trust types

http://technet.microsoft.com/en-us/library/cc775736%28v=ws.10%29.aspx

This will help you to Determine Trust Relationship Configurations

http://support.microsoft.com/kb/228477

Prerequisite: You need to be a member of the Domain Admins group in order to create and manage trust relationships. You need to have “very good” DNS to perform this otherwise this relation will brake drastically un evenly.

As I said, DNS must be setup properly between the two domains either by using conditional forwarders, active directory federation services or Stub zone.  For the purpose of this document I will setup a stub zone, it is also recommended to check and verify the forest functional level which should be same for both domains.  Be smart in this to confirm the prerequisite, don’t underestimate confident with prerequisites, this is the place where experts collapse. Be a hero don’t forget.

Frist we will configure the Stub Zone in DNS for both the domain forest. There is a good article please understand and proceed. Don’t take anybody’s advice…

http://technet.microsoft.com/en-us/library/cc728412%28v=ws.10%29.aspx

When I was preparing this document I face some issue with FSMO roles of domain which involves in this domain trust. I had a hard time to transfer and seize the Roles which is out of the scope of this; I will discuss this in a separate post.

To create a Stub Zone, Go to DNS management then right click on the Forward Lookup Zone and Click New Zone for the first domain controller which is going to be trusted with other domain in the other forest.you will be prompted with a screen similar to the one below.

clip_image001

Then Click Next.

clip_image002

We will have some brief description about different type of Zone offered by this Next click. We will select Stub Zone. Don’t forget the check Box below Stub zone.

clip_image003

Because of the check mark Store the zone in Active Directory in the last step.  Select an option which is applicable to and click Next.

clip_image004

Specify the name of the domain which you are going to trust we can say the other domain in the other forest. It will be the name of your Zone in DNS.

clip_image005

Here we have to specify the IP or domain name of our other domain which we are going to trust. The check box once enabled, Use the above servers to create a local list of master servers will get a list of all other DNS servers and next will be the overview of your new trust. And click finish.

clip_image006

The Same step you follow to the other domain which you are going to trust to create a stub zone.

Once this Zone creation is done on both the domain DNS in different forest.

Go to Administrative tools, active Directory domain and Trust and right click on the Domain and click Properties.

clip_image007

Click New Trust

clip_image008

Click Next.

clip_image009

Type the name of the domain which we are going to create the trust. And click Next.

clip_image010

Choose the trust type. (Details are linked in the earlier part)

clip_image011

Select the direction of the trust which is applicable for your need. (consider the security and trust resource outcome)

clip_image012

Here you can specify trust relation side whether it I local or both domain and local always had a tendency for Both this domain and the specified domain which I again environment specific.

clip_image013

Supply administrative credential for the appropriate domain to trust.

clip_image014

This option is also environment specific and select according to what you are looking for.

clip_image015

This option is also same and environment specific and choose according to your goal.

clip_image016

Overview of the trust options we have selected.

clip_image017

Next and reset of the few windows will confirm all the trust

clip_image018

This will finish the trust wizard.

clip_image019

Here is the final screen for the confirmation. And the validate button will allow you to check the trust relation between the domain.

clip_image020

clip_image021

Use proper credential while validating the trust which you have created.

clip_image022

This option will appear if you lose connectivity or some other issues; we can reset the trust password here.

clip_image023

If all went right then on the Active Directory Users and Computer you will see the other trusted domain and its resources.

I hope this article will get a clear idea about how to create a domain trust. Waiting for you feed back.