“Microsoft Technet Guru – Gold Medal Winning Article- Nov, 2016”

Abstract

Monitoring disk space utilization of server(s) is the critical and important job for any administrator. Keeping things organized might improve application availability and server availability. This article takes us through the in-detail steps to read each drive and notify every drive details based on threshold values and output data.You’ll basically feed a list of servers to watch over, and it will report back on these for you, meaning you could also use it as a more general “daily server disk space report”.A DBA doesn’t want to run out of space on their servers, even in their labs! To avoid this happening, wrote a PowerShell script to provide some alerts by email.


Introduction

This article talks about the use of credentials. The credentials can be used to query external servers which have the trust relationship between the domains. Also, list various methods to secure the password. The process iterates through a list of servers and drives that you have listed in a csv file. Checking for disk space status of every listed drive and its status may fall under one of the four statuses that are defined as critical, warning, low and good. If the disk in a question of below the threshold then the corresponding status is updated and notification sent to the respective teams.


Highlights

  • The CSV input  – The file contains server, disk information and threshold values
  • The WMI class, Win32_LogicalDisk querying with credentials and without credentials
  • Activity logging in a log file
  • The output has status columns which give a clear indication of status of each drive
  • Email notification


Querying – Win32_LogicalDisks

  • Using Credentials
  • Without using Credentials

Using Credentials

Get-Credential always pop-up dialog box for entering a password, however, you can save your securestring password to a file or directly feed the password. The problem with this is that the password will be exposed to anyone with access to the file.

  • Using Get-Credential cmdlet – Pop up dialog box
  • Directly using password
  • Using secured file

Using Get-Credential cmdlet – Pop dialog box

The Get-Credential displays a window to enter credential details. This will appear every time when you run the script.The $credential variable store the username and password. It’s then fed to the respective queries for further processing.

clear
$credential = Get-Credential
foreach ( $args in get-Content c:\server.txt ) {
get-WmiObject win32_logicaldisk -Credential $credential -ComputerName $args -Filter "Drivetype=3"  |
ft SystemName,DeviceID,VolumeName,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
}

Hard code the credentials

The password is hard coded in the script. Of course, the problem with this is that your password will be exposed to anyone with access to the script file.

$User = 'hqnt\abcd'
 $Pass = ConvertTo-SecureString 'abcd@2015' -AsPlainText -Force
 $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Pass
 foreach ( $args in get-Content c:\server.txt ) {
get-WmiObject win32_logicaldisk -ComputerName $args -Credential $Credentials -Filter "Drivetype=3"  |
ft SystemName,DeviceID,VolumeName,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
}

Using Secured file

First, Password has to be written to a file

ps:\>read-host -AsSecureString |ConvertFrom-SecureString |Out-File C:\SecurePassword.txt

Second, The credentials are read from the file using PSCredential class. You don’t need to re-enter the password over and over again.

clear
$User = 'hqnt\abcdv'
$pass= cat C:\passwordstring.txt |ConvertTo-SecureString
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Pass
foreach ( $args in get-Content c:\server.txt ) {
get-WmiObject win32_logicaldisk -ComputerName $args -Credentials $cred -Filter "Drivetype=3"  |
 
ft SystemName,DeviceID,VolumeName,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
 
}

 


Without using Credentials

You don’t need to use the credential parameter in any of the cmdlet execution.
clear
foreach ( $args in get-Content c:\server.txt ) {
get-WmiObject win32_logicaldisk -ComputerName $args -Filter "Drivetype=3"  |
ft SystemName,DeviceID,VolumeName,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
}

 


Download

Download the source code from the Technet Gallery

PowerShell : Monitoring and Notification – Disk Space Alerts

 


Code in detail

This section describes the coding and other details of the PoSH script – diskSpace.ps1.

Input File

The template of Inputserver.csv is given below. Change the content as per your requirement/environment

import-csv C:\InputServer_1.csv |Format-Table -AutoSize

The comma-delimited text file (.csv) file InputServer_1.csv file is parsed below. You’ll notice at the top that there are headings for each column: ServerName, Drive, LowTh, WarnTh,CritTh,Email, LowPri, WarnPri, CritPri, and EscInst. The PowerShell import-csv cmdlet understands this default format and expects to see the column headers before parsing the main data.

  • ServerName – Name of the server – acts as a source for cmdlet execution
  • Drive – The drive letter is an input for querying Win32_LogicalDisk class library
  • LowTh – The low limit for sending the alert notification to intended recipients
  • WarnTh – The Middle limit for the drive
  • CritTh – Higher limit for the drive and it requires urgent attention from the technician
  • Email – The list where the notification is sent
  • LowPri – The Priority decides the attention and importance . In this case, the technician will be having enough time to respond to this issue
  • WarnPri -The Priority decides the attention and importance . In this case, the technician will fix turn around time to react to any such issues
  • CritPri  – Requires urgent and immediate action
  • EscInst – Defines the escalation team.

The above columns are defined just to illustrate how the process works. This can vary for every environment by considering the underlying infrastructure

Write-Log

Write-Log writes a message to a specified log file along with the current time stamp also writes state of the message(Information, Warning or Error).

For example, The first example writes a simple message with a default state to a log file abc.log. In the second example, a message along with “Error” state details are entered into the log file.

1.EXAMPLE 1
2.PS:\> Write-Log  -Message "Server is reachable and starting the process " -Logfile c:\PowerSQL\abc.log
3.EXAMPLE 2
4.PS:\> Write-Log  -level Error -Message "Server is not reachable " -Logfile c:\PowerSQL\abc.log

The below function can be reused to in any of the PoSH code. Also, the output file will be used for troubleshooting and activity progress tracking.

01.Function Write-Log {
02.    [CmdletBinding()]
03.    Param(
04.    [Parameter(Mandatory=$False)]
05.    [ValidateSet("INFO","WARN","ERROR")]
06.    [String]
07.    $Level = "INFO",
08. 
09.    [Parameter(Mandatory=$True)]
10.    [string]
11.    $Message,
12. 
13.    [Parameter(Mandatory=$False)]
14.    [string]
15.    $logfile
16.    )
17. 
18.    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
19.    $Line = "$Stamp $Level $Message"
20.    If($logfile) {
21.    Add-Content $logfile -Value $Line
22.    }
23.    Else {
24.        Write-Output $Line
25.    }
26.}

Password

This portion of code decides whether to pass credentials or not.  The Get-credential always pop-up dialog box for entering a password, however, you can save your securestring password to a file or directly feed the password. The problem with this is that the password will be exposed to anyone with access to the file. If you want to use the default login credentials then you don’t need to mention anything in the code. You can comment the line of code.

$User = 'abcd'
$Pass = ConvertTo-SecureString ''abcd@#2016' -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Pass

Import Server and Drive details

This part of the code read values from CSV file. The CSV file has well-defined disk space threshold values for each drive that needs monitoring and alerting. The code imports the server name and reads server header value and loop through each and every server. After reading, the values are assigned to a local variable and then it’s used to query WMI query by filtering on -computername and -DeviceID which is marked green in color. The credentials parameter would be really helpful when you are querying different domains which has trust relationships in between. If you are querying the servers that are under the same domain and want to use the default login credentials then you can ignore the credential parameter.
The next part is to calculate the “free %”. The status column is populated based on input threshold values. The $percentageFree will be used to identify the status of the drive. The four status of each drives are Critical, Warning, Low and Good. The three input parameters $clowth, $cwarnth,$ccritth compared with $percentageFree variable to yield a result for status columns.

01.#Import the file to get the drives status and other usage details
02.
03.#The Import-Csv cmdlet provides a way for you to read in data from a comma-separated values file (CSV)
04.   
05.Import-Csv $InputServer|%{
06.$cserver = $_.Server
07.$cdrivelt = $_.Drive
08.$clowth = $_.LowTh
09.$cwarnth = $_.WarnTh
10.$ccritth = $_.CritTh
11.$cemail = $_.Email
12.$clowpri = $_.LowPri
13.$cwarnpri = $_.WarnPri
14.$ccritpri = $_.CritPri
15.$cescinst = $_.EscInst
16.If (!(Test-Connection $_.Server -count 1 -quiet)) {
17.#Write the message to the log file
18.Write-Log  -level ERROR -Message "$($_.Server) is not reachable" -Logfile $Logfile
19.}
20.else
21.{
22.#Write the Progress to log file
23.Write-Log  -Message "$($_.Server) is reachable and starting the process " -Logfile $Logfile
24. 
25.$diskinfo= Get-WmiObject -Class Win32_LogicalDisk -ComputerName $cserver  -Filter"DeviceID='$cdrivelt'"
26.ForEach ($disk in $diskinfo)
27.{
28.#Calculate the % free. This parameter will be compared with various thresholds to derive the status of the drive
29.If ($diskinfo.Size -gt 0) {$percentFree = [Math]::round((($diskinfo.freespace/$diskinfo.size) * 100))}
30.Else {$percentFree = 0}
31.    #Determine if disk needs to be flagged for warning or critical alert
32.    If ($diskinfo.Size -gt 0) {$percentFree = [Math]::round((($diskinfo.freespace/$diskinfo.size) * 100))}
33.    Else {$percentFree = 0}
34. If ($percentFree -le  $ccritth) {
35. 
36. $status = "Critical"
37. $priority = $ccritpri
38. $body = @"
39.Notification that a disk drive is reporting an alert for low disk space!
40.$cserver $cdrivelt has $percentFree % free space. Please assign an $priority priority ticket to the $cescinst team.
41.-This is an automated email being generated by the script DiskMonCheck.ps1, as a scheduled task on HQMONP09.
42."@
43.Send-MailMessage -to $cemail -from "HQMONP09@appvion.com" -Subject "Disk Alert - $cserver $cdrivelt out of disk space!" -body $body -smtpserver $SMTPServer
44.Write-Log  -Message "$($_.Server) Critical alert logged for the drive $cdrivelt " -Logfile $Logfile
45.
46.}

Output

This section describes various options available in the script to validate the disk space data

Log filename

This below code defines the output log file location, and directory to save the output. The $date variable hold the date formatting part. It’s then appended to the $logfilename to generate a more meaningful filename. For example, DiskSpaceLog_2016-10-10

$date=Get-Date -format "yyyy-MM-d"
#Prepare log file and output CSV file
$LogFileName="DiskSpaceLog_$($date)"

Email

The section defines the body of an email, the content prepared using here-string. The email gives a detailed information about the severity of the space and notifies the technician to act to remediate the issue. Here-Strings are a great technique to use if you want to have a lot of text that covers several lines. The below is one of the example created for the body of the email auto-notification.

$body = @"
Notification that a disk drive is reporting an alert for low disk space!
$cserver $cdrivelt has $percentFree % free space. Please assign an $priority priority ticket to the $cescinst team.
-This is an automated email being generated by the script DiskMonCheck.ps1, as a scheduled task on HQMONP09.
"@
Send-MailMessage -to $cemail -from "HQMONP09@appvion.com" -Subject "Disk Alert - $cserver $cdrivelt out of disk space!" -body $body -smtpserver $SMTPServer

Console

The output is customized and written to the console. Have given this option validate the data with the email. In the console output, the columns might not be displayed in the order in which we construct the script.  The output of an object to be displayed in a certain order because PoSH rearranges the output as per available metadata hence the below code describes one way of showing the customization.

$mydisk +=New-Object PSObject -Property @{
    Server=$_.Server
    DeviceID= $disk.DeviceID
    VolumeName= $disk.VolumeName
    Size= [math]::Round(($disk.Size /1GB),2)
    Usedspace= [math]::Round((($disk.Size - $disk.FreeSpace)/1GB),2)
    Percentage= ("{0:P}" -f ($disk.FreeSpace / $disk.Size))
    status=$status
    }
}
}
}
$mydisk |Select-Object @{Name="Server";Expression={$_.Server}},@{Name="DeviceID";Expression={$_.DeviceID}},
 @{Name="VolumeName";Expression={$_.VolumeName}},
 @{Name="Size";Expression={$_.Size}},
 @{Name="Used Space";Expression={$_.Usedspace}},
 @{Name="% Free";Expression={$_.Percentage}},
 @{Name="Status";Expression={$_.status}}|Format-Table  -AutoSize
 

 


Code

<#
.Synopsis
The objective of the script is to make use of .csv files as sources for various parts of the script.
 .Description
  Function to log manipulate the date based on the input file and display it to console. Log entries in the log file are time stamped. By default the message are logged under INFO category. It can be changed to other category such as "WARN" and "Error" using -level parameter
 .Parameter InoutFile
  Path to the file where the input details are saved.
  Example: c:\InputServer.csv
 
 .Parameter SMTPServer
  The SMTP server name to send email to respective intendencies
  Example: ancd.gmail.com
 .Example
   Write-Log  -Message "$($_.Server) is reachable and starting the process " -Logfile $Logfile
  
 .Example
  Write-Log  -Message "$($_.Server) Critical alert logged for the drive $cdrivelt " -Logfile $Logfile
 .Link
 .Notes
 The CSV file is going to hold all the metadata for each drive that you intend to monitor and send out notification
  
#>
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Low')]
    Param(
        [Parameter(Mandatory=$true,
                  Position=0)]
            [String]$InputServer,
        [Parameter(Mandatory=$true,
                  Position=1)]
            [String]$DirectorytoSave,
        [Parameter(Mandatory=$true,
                   Position=2)]
            [String]$SMTPServer
    )
 
# formatting the date
 
 $date=Get-Date -format "yyyy-MM-d"
  
 #Prepare log file and output CSV file
  
 $LogFileName="DiskSpaceLog_$($date)"
   
# before we do anything else, are we likely to be able to save the file?
# if the directory doesn't exist, then create it
 
if (!(Test-Path -path "$DirectoryToSaveTo")) #create it if not existing
  {
  New-Item "$DirectoryToSaveTo" -type directory | out-null
  }
#log File creation   
 
$logfile = "$DirectoryToSave$LogFileName.log"
 
if (!(Test-Path -path "$logfile")) #create it if not existing
  {
   New-Item -ItemType file $logfile -Force
  }
 
# Prepare headers for the log file for each execution of script
 
Add-Content $logfile "#################################################################"
Add-Content $logfile "Disk Space Details"
Add-Content $logfile "Generated $(get-date)"
Add-Content $logfile "Generated from $(gc env:computername)"
Add-Content $logfile "#################################################################"
Function Write-Log {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$False)]
    [ValidateSet("INFO","WARN","ERROR")]
    [String]
    $Level = "INFO",
 
    [Parameter(Mandatory=$True)]
    [string]
    $Message,
 
    [Parameter(Mandatory=$False)]
    [string]
    $logfile
    )
 
    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    $Line = "$Stamp $Level $Message"
    If($logfile) {
    Add-Content $logfile -Value $Line
    }
    Else {
        Write-Output $Line
    }
}
 
   
#Creating PowerShell custom objects
 
$Mydisk=@() 
 
#Import the file to get the drives status and other usage details
#The Import-Csv cmdlet provides a way for you to read in data from a comma-separated values file (CSV)
   
Import-Csv $InputServer|%{
$cserver = $_.Server
$cdrivelt = $_.Drive
$clowth = $_.LowTh
$cwarnth = $_.WarnTh
$ccritth = $_.CritTh
$cemail = $_.Email
$clowpri = $_.LowPri
$cwarnpri = $_.WarnPri
$ccritpri = $_.CritPri
$cescinst = $_.EscInst
If (!(Test-Connection $_.Server -count 1 -quiet)) {
#Write the message to the log file
Write-Log  -level ERROR -Message "$($_.Server) is not reachable" -Logfile $Logfile
}
else
{
#Write the Progress to log file
Write-Log  -Message "$($_.Server) is reachable and starting the process " -Logfile $Logfile
 
$diskinfo= Get-WmiObject -Class Win32_LogicalDisk -ComputerName $cserver  -Filter "DeviceID='$cdrivelt'"
ForEach ($disk in $diskinfo)
{
#Calculate the % free. This parameter will be compared with various thresholds to derive the status of the drive
If ($diskinfo.Size -gt 0) {$percentFree = [Math]::round((($diskinfo.freespace/$diskinfo.size) * 100))}
Else {$percentFree = 0}
    #Determine if disk needs to be flagged for warning or critical alert
    If ($diskinfo.Size -gt 0) {$percentFree = [Math]::round((($diskinfo.freespace/$diskinfo.size) * 100))}
    Else {$percentFree = 0}
 If ($percentFree -le  $ccritth) {
 
 $status = "Critical"
 $priority = $ccritpri
 $body = @"
Notification that a disk drive is reporting an alert for low disk space!
$cserver $cdrivelt has $percentFree % free space. Please assign an $priority priority ticket to the $cescinst team.
-This is an automated email being generated by the script DiskMonCheck.ps1, as a scheduled task on HQMONP09.
"@
Send-MailMessage -to $cemail -from "HQMONP09@appvion.com" -Subject "Disk Alert - $cserver $cdrivelt out of disk space!" -body $body -smtpserver $SMTPServer
Write-Log  -Message "$($_.Server) Critical alert logged for the drive $cdrivelt " -Logfile $Logfile
}
 
 ElseIf ($percentFree -gt $ccritth -AND $percentFree -le $cwarnth) {
 $status = "Warning"
 $priority = $cwarnpri
$body = @"
Notification that a disk drive is reporting an alert for low disk space!
$cserver $cdrivelt has $percentFree % free space. Please assign a $priority priority ticket to the $cescinst team.
-This is an automated email being generated by the script DiskMonCheck.ps1, as a scheduled task on HQMONP09.
"@
Send-MailMessage -to $cemail -from "HQMONP09@appvion.com" -Subject "Disk Alert - $cserver $cdrivelt disk space warning!" -body $body -smtpserver $SMTPServer
Write-Log  -Message "$($_.Server) Warning alert logged for the drive $cdrivelt " -Logfile $Logfile
}
ElseIf ($percentFree -ge $cwarnth -AND $percentFree -lt $clowth) { $status = "Low"
$priority = $clowpri
$body = @"
Notification that a disk drive is reporting an alert for low disk space!
$cserver $cdrivelt has $percentFree % free space. Please assign a $priority priority ticket to the $cescinst team.
-This is an automated email being generated by the script DiskMonCheck.ps1, as a scheduled task on HQMONP09.
"@
Send-MailMessage -to $cemail -from "HQMONP09@appvion.com" -Subject "Disk Alert - $cserver $cdrivelt disk space warning!" -body $body -smtpserver $SMTPServer
Write-Log  -Message "$($_.Server) low alert logged for the drive $cdrivelt " -Logfile $Logfile
}
              
Else { $status = "Good" }
 
$mydisk +=New-Object PSObject -Property @{
    Server=$_.Server
    DeviceID= $disk.DeviceID
    VolumeName= $disk.VolumeName
    Size= [math]::Round(($disk.Size /1GB),2)
    Usedspace= [math]::Round((($disk.Size - $disk.FreeSpace)/1GB),2)
    Percentage= ("{0:P}" -f ($disk.FreeSpace / $disk.Size))
    status=$status
    }
}
}
}
$mydisk |Select-Object @{Name="Server";Expression={$_.Server}},@{Name="DeviceID";Expression={$_.DeviceID}},
 @{Name="VolumeName";Expression={$_.VolumeName}},
 @{Name="Size";Expression={$_.Size}},
 @{Name="Used Space";Expression={$_.Usedspace}},
 @{Name="% Free";Expression={$_.Percentage}},
 @{Name="Status";Expression={$_.status}}|Format-Table  -AutoSize
  

Output

Compare Mail Inbox view with PoSH output

Log file details are shown below

Running DiskSpace.ps1 file with parameters

  • InputServer – The source for the entire script
  • DirectoryToSave – To save activity and progress of the script file into a file
  • SMTPServer – Email notification

Conclusion

This article has illustrated how to set up and configure email notification when Server is running out of Hard Disk Space. Setting up a simple alerting system like this (i.e. via a schedule SQL Server agent job or via Task Scheduler) is a great way to help ensure that you don’t run into any surprises.

As usual, any feedback is welcome, and I hope that this article was helpful to you!

 


References

Technet

  1. PoSH : CSV-DiskSpace- CSV 
  2. PoSH : Disk Space Utilization Report
  3. PoSH : CSV – Disk Space Report – HTML
  4. PoSH : CSV – Disk Space Report – Excel
  5. PoSH : DiskSpace GUI Tool
  6. PoSH : MultiServer(s) Disk Space GUI Tool
  7. PoSH & SQL : Monitoring Disk Space with SQL Server and PowerShell via SQL Agent


See Also