Powershell – Free to use and easy to monitor with – Drive Space Notifications

Logo-PowerShell

Caught this one in the Technet forums.  How do you monitor for drive space and Notify?

This is where I absolutely fall head over heals in love with Powershell.   It’s so EASY to write a way to monitor and list the information!  It’s FREE MONITORING SOFTWARE with a BUILT IN Emailing system!

So let’s say you want to know about hard drives in your computer.    All of that information is available via WMI in the win32-volumes

So running a GET-WMIOBJECT win32-volumes will show you all the physical drives attached.   Cdroms, EISA partitions etc.

But do we care about CDrom drive free space?

Nope.  But It’s not difficult to get what we want from WMI with Powershell.

 

GET-WMIOBJECT WIN32-VOLUME | select-object freespace, capacity, drivetype, driveletter

 

Will show us all the details we’re looking for.  And the cool part is drive letter is labelled EXACTLY the way it should be “C:” “D:” “Z:”
”freespace” is in raw bytes as is the “capacity” property but that’s fine.    You can always format something like that.  The important part is to get the information

And again you can say your freespace cutoff is 100 bytes, 100 kilobytes, 20 gigabytes or just have it factored in as a percentage of the free space.

 

$driveinfo=get-wmiobject win32-volume | where { $-.driveletter -eq ‘C:’ } | select-object freespace, capacity, drivetype, driveletter
$Percent=.10
$WarningLevel=$driveinfo.capacity *  $Percent

 

Now in Powershell V2 you have the ability to send mail straight from the Script.  It’s a built in command that only requires an SMTP server to point to and can attach files as well.  All in a SINGLE line!

send-mailmessage -from "Server01 <server01@contoso.local>" -to "MyAdministrator <networkAdmin@contoso.local>" -subject "I’m all out of space" -body "I’m all out of space, I’m so lost without you, please get me some more now, the users are crying…I have $driveinfo.freespace bytes leftover! Hurry!" -priority High -dno onSuccess, onFailure -smtpServer smtp.contoso.local

 

The message can be whatever YOU want in the body, same of course for the addresses.   Just typing a HELP SEND-MAILMESSAGE –EXAMPLES in Powershell gives you some dead simple ones to work from, covering almost all emailing examples!

With no effort we now have a basic script to monitor free space on a hard drive and to email somebody when it gets low.   You don’t need SCOM or expensive monitoring software to do this.  Just Powershell V2 (Free) and a little time! And it works ALL the WAY DOWN TO XP/Server 2003!

——————- FreeDriveSpaceWarning.PS! —————————-

# Change C: to whichever letter you’d like monitored

$driveinfo=get-wmiobject win32-volume | where { $-.driveletter -eq ‘C:’ } | select-object freespace, capacity, drivetype, driveletter

# 10 percent 10/100

$Percent=.10

# XXX % of maximum space is our warning level
$WarningLevel=$driveinfo.capacity *  $Percent

if ($driveinfo.freespace -lt $WarningLevel)
{
send-mailmessage -from "Server01 <server01@contoso.local>" -to "MyAdministrator <networkAdmin@contoso.local>" -subject "I’m all out of space" -body "I’m all out of space, I’m so lost without you, please get me some more now, the users are crying…I have $driveinfo.freespace bytes leftover! Hurry!" -priority High -dno onSuccess, onFailure -smtpServer smtp.contoso.local
}

——————- FreeDriveSpaceWarning.PS! —————————-

There you go.  

Free for the Holidays! 🙂

Sean
The Energized Tech

Leave a Reply