Need a Powershell script to that will monitor disk space usage and alert on low disk.

I need a powershell script that will monitor disk usage for our Windows Servers 2008, 2012 environment (only 4) with multiple partitions, and send out an alert when it gets below a certain threshold via email.

The alert needs to contain the server name, what percentage is left, and how much space is being utilized, what how much space is free.

I'm thinking to run it on a schedule task every few hours.
tools2teachAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darrell PorterEnterprise Business Process ArchitectCommented:
Have you considered using Netwrix's Free Diskspace Monitor?

OR

Create a simple .cmd script to send the warning email, making sure to include %1 so as to include any command line arguments produced by perfmon.exe.

If you don’t have any commmand line arguments when you set up alerts the script will not be called:

Using bmail.exe from Beyond Logic

c:\bmail.exe -s mail.server.ip -t email@addresstosendto.com -f email@addresstosendfrom -h -a “Disk Space Warning on server.name.com” -b “Less than 100GB free on server.name.com. Please free space to ensure weekly backups can continue. Free: %1 MB”
 
The direct path to the executable must be explicitly stated, otherwise it will not be called and no error/warning will be shown.
 
Now open perfmon.exe, expand “Performance Logs and Alerts” – “Alerts”. Right click and select “New Alert Settings…” and choose a name. The comment can be anything.
 
Now add a counter. I used “LogicalDisk” – “Free Megabytes” – “C:” to select free space on the C drive. Set “Alert when the value is:” to “Under” and the “Limit:” to whatever free space warning you need.
Set the sample interval to 3 hours. Now select run as to be an administrator on the machine and set the password.
 
Click the action tab and select “Run this program:” and put in the path to your .cmd file, in this case “C:emailfromcmdemailteam.cmd”. Select “Measured value” in “Command Line Arguments…”. Now click OK, you may need to enter the password for the user with permission to run your .cmd.
 
Now your server will email you when drive space gets low.
Mark GalvinManaging Director / Principal ConsultantCommented:
Hi

I use this in my environment and it works a treat (saved as a .ps1 file):
# First list all the servers that you want to have in the report, separate with a comma
$serverlist = ("SERVER1","SERVER2")
# Set the 'warning' and 'critcal' percentages levels
$warning = 25 
$critical = 15 
New-Item -ItemType file $freeSpaceFileName -Force 
# Getting the freespace info using WMI 
# Get-WmiObject win32_logicaldisk  | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt 
# Function to write the HTML Header to the file 
$smtpHost = "SMTPHOSTNAME"
$emailFrom = "FROM EMAIL"
$emailTo = "TO EMAIL"
Function writeHtmlHeader 
{ 
param($fileName) 
$date = ( get-date ).ToString('dd/MM/yyyy') 
Add-Content $fileName "<html>" 
Add-Content $fileName "<head>" 
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>" 
Add-Content $fileName '<title>DiskSpace Report</title>' 
add-content $fileName '<STYLE TYPE="text/css">' 
add-content $fileName  "<!--" 
add-content $fileName  "td {" 
add-content $fileName  "font-family: Tahoma;" 
add-content $fileName  "font-size: 11px;" 
add-content $fileName  "border-top: 1px solid #999999;" 
add-content $fileName  "border-right: 1px solid #999999;" 
add-content $fileName  "border-bottom: 1px solid #999999;" 
add-content $fileName  "border-left: 1px solid #999999;" 
add-content $fileName  "padding-top: 0px;" 
add-content $fileName  "padding-right: 0px;" 
add-content $fileName  "padding-bottom: 0px;" 
add-content $fileName  "padding-left: 0px;" 
add-content $fileName  "}" 
add-content $fileName  "body {" 
add-content $fileName  "margin-left: 5px;" 
add-content $fileName  "margin-top: 5px;" 
add-content $fileName  "margin-right: 0px;" 
add-content $fileName  "margin-bottom: 10px;" 
add-content $fileName  "" 
add-content $fileName  "table {" 
add-content $fileName  "border: thin solid #000000;" 
add-content $fileName  "}" 
add-content $fileName  "-->" 
add-content $fileName  "</style>" 
Add-Content $fileName "</head>" 
Add-Content $fileName "<body>" 
 
add-content $fileName  "<table width='100%'>" 
add-content $fileName  "<tr bgcolor='#CCCCCC'>" 
add-content $fileName  "<td colspan='7' height='25' align='center'>" 
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>Intrust Server Disk Space Report - $date</strong></font>" 
add-content $fileName  "</td>" 
add-content $fileName  "</tr>" 
add-content $fileName  "</table>" 
 
} 
 
# Function to write the HTML Header to the file 
Function writeTableHeader 
{ 
param($fileName) 
 
Add-Content $fileName "<tr bgcolor=#CCCCCC>" 
Add-Content $fileName "<td width='10%' align='center'>Drive</td>" 
Add-Content $fileName "<td width='50%' align='center'>Drive Label</td>" 
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>" 
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>" 
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>" 
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>" 
Add-Content $fileName "</tr>" 
} 
 
Function writeHtmlFooter 
{ 
param($fileName) 
 
Add-Content $fileName "</body>" 
Add-Content $fileName "</html>" 
} 
 
Function writeDiskInfo 
{ 
param($fileName,$devId,$volName,$frSpace,$totSpace) 
$totSpace=[math]::Round(($totSpace/1073741824),2) 
$frSpace=[Math]::Round(($frSpace/1073741824),2) 
$usedSpace = $totSpace - $frspace 
$usedSpace=[Math]::Round($usedSpace,2) 
$freePercent = ($frspace/$totSpace)*100 
$freePercent = [Math]::Round($freePercent,0) 
 if ($freePercent -gt $warning) 
 { 
 Add-Content $fileName "<tr>" 
 Add-Content $fileName "<td>$devid</td>" 
 Add-Content $fileName "<td>$volName</td>" 
 
 Add-Content $fileName "<td>$totSpace</td>" 
 Add-Content $fileName "<td>$usedSpace</td>" 
 Add-Content $fileName "<td>$frSpace</td>" 
 Add-Content $fileName "<td>$freePercent</td>" 
 Add-Content $fileName "</tr>" 
 } 
 elseif ($freePercent -le $critical) 
 { 
 Add-Content $fileName "<tr>" 
 Add-Content $fileName "<td>$devid</td>" 
 Add-Content $fileName "<td>$volName</td>" 
 Add-Content $fileName "<td>$totSpace</td>" 
 Add-Content $fileName "<td>$usedSpace</td>" 
 Add-Content $fileName "<td>$frSpace</td>" 
 Add-Content $fileName "<td bgcolor='#FF0000' align=center>$freePercent</td>" 
 #<td bgcolor='#FF0000' align=center> 
 Add-Content $fileName "</tr>" 
 } 
 else 
 { 
 Add-Content $fileName "<tr>" 
 Add-Content $fileName "<td>$devid</td>" 
 Add-Content $fileName "<td>$volName</td>" 
 Add-Content $fileName "<td>$totSpace</td>" 
 Add-Content $fileName "<td>$usedSpace</td>" 
 Add-Content $fileName "<td>$frSpace</td>" 
 Add-Content $fileName "<td bgcolor='#FBB917' align=center>$freePercent</td>" 
 # #FBB917 
 Add-Content $fileName "</tr>" 
 } 
} 
Function sendEmail 
{ param($from,$to,$subject,$smtphost,$htmlFileName) 
$body = Get-Content $htmlFileName 
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body 
$msg.isBodyhtml = $true 
$smtp.send($msg) 
 
} 
 
writeHtmlHeader $freeSpaceFileName 
foreach ($server in $serverlist) 
{ 
 Add-Content $freeSpaceFileName "<table width='100%'><tbody>" 
 Add-Content $freeSpaceFileName "<tr bgcolor='#CCCCCC'>" 
 Add-Content $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server </strong></font></td>" 
 Add-Content $freeSpaceFileName "</tr>" 
 
 writeTableHeader $freeSpaceFileName 
 
 $dp = Get-WmiObject win32_logicaldisk -ComputerName $server |  Where-Object {$_.drivetype -eq 3} 
 foreach ($item in $dp) 
 { 
 Write-Host  $item.DeviceID  $item.VolumeName $item.FreeSpace $item.Size 
 writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size 
 
} 
  Add-Content $freeSpaceFileName "</table>" 
}  
writeHtmlFooter $freeSpaceFileName 
$date = ( get-date ).ToString('dd/MM/yyyy') 
sendEmail $emailFrom  $emailTo "Server Disk Space Report - $Date" $smtpHost $freeSpaceFileName 

Open in new window


You need to add your servers here : $serverlist = ("SERVER1","SERVER2")
Change these details to match your environment:
$smtpHost = "SMTPHOSTNAME"
$emailFrom = "FROM EMAIL"
$emailTo = "TO EMAIL"

I run this using a Scheduled Task and get a nicely formatted email each day.

Hope this helps
Thanks
Mark

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tools2teachAuthor Commented:
^^^
This is perfect.  Getting the email but it's blank.

I get an error when it tries to create the HTML page

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:12 char:13
+  Add-Content <<<<  $fileName "<tr>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:13 char:13
+  Add-Content <<<<  $fileName "<td>$devid</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:14 char:13
+  Add-Content <<<<  $fileName "<td>$volName</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:15 char:13
+  Add-Content <<<<  $fileName "<td>$totSpace</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:16 char:13
+  Add-Content <<<<  $fileName "<td>$usedSpace</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:17 char:13
+  Add-Content <<<<  $fileName "<td>$frSpace</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:18 char:13
+  Add-Content <<<<  $fileName "<td>$freePercent</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:19 char:13
+  Add-Content <<<<  $fileName "</tr>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:14 char:14
+   Add-Content <<<<  $freeSpaceFileName "</table>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:3 char:13
+  Add-Content <<<<  $freeSpaceFileName "<table width='100%'><tbody>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:4 char:13
+  Add-Content <<<<  $freeSpaceFileName "<tr bgcolor='#CCCCCC'>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:5 char:13
+  Add-Content <<<<  $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399'
size='2'><strong> $server </strong></font></td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:13
+  Add-Content <<<<  $freeSpaceFileName "</tr>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:4 char:12
+ Add-Content <<<<  $fileName "<tr bgcolor=#CCCCCC>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:5 char:12
+ Add-Content <<<<  $fileName "<td width='10%' align='center'>Drive</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:12
+ Add-Content <<<<  $fileName "<td width='50%' align='center'>Drive Label</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:7 char:12
+ Add-Content <<<<  $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:8 char:12
+ Add-Content <<<<  $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:9 char:12
+ Add-Content <<<<  $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:10 char:12
+ Add-Content <<<<  $fileName "<td width='10%' align='center'>Freespace %</td>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand

Add-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:11 char:12
+ Add-Content <<<<  $fileName "</tr>"
    + CategoryInfo          : InvalidData: (:) [Add-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentC
   ommand
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.