Link to home
Start Free TrialLog in
Avatar of tools2teach
tools2teach

asked on

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.
Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Mark Galvin
Mark Galvin
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tools2teach
tools2teach

ASKER

^^^
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