With a command line I noticed that my free disk space is below some value – I


scan 17

This morning, I was browsing through Twitter, when I saw our teacher Brent Ozar (blog | twitter) give a tip about checking the disk usage , using  the script of our friend and PowerSheller Aaron Nelson (blog | twitter)

You can view this nice script here –> Quick Blog: PowerShell Disk and MountPoint Check

And if I said you can be notified if the free space is below of , let´s say 90 MB, with a powershell command line?          

I am with 80GB free space in my drive C:, so I put to be notified if it is below 90GB :

   1: Register-WMIEvent -Query "Select * from __instanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfDisk_LogicalDisk' AND TargetInstance.NAME='C:' AND TargetInstance.freemegabytes <90000" -Action {Write-Host "Hey..hey..wakeup Man. Take a look in Your Disk C, it is almost full"} 

Remember that freemegabytes is in MB , so in my case  90000 MB.

and the output is

image

Ok ..This is so cool.

But surely you do not want this annoying message every 5 seconds.
Ha, How can I know it’s every 5 seconds?. Simple, the keyword WHITIN in WQL.
In the words of my good friend and PowerShell Jedi, Ravikanth Chaganti (blog | twitter), “WITHIN keyword is used to Specify the polling interval or the grouping interval specified number of seconds and is a floating point number.”

Well, if you STILL do not know what is WQL, I strongly suggest to download

Ravikanth Chaganti´s “WMI Query Language via PowerShell”,

But back to the subject, so just change to 3600 , to be reporting every hour.

Sending Email

As you probably know, there is a cmdlet in PowerShell 2.0 to send email (Send-MailMessage) , but I use Jakob Bindslet´s Send-SMTPmail (JBs Powershell) , but this is simply personal, because I like to use it.

So, with the Send-SMTPmail loaded into your User Profile, we can do :

   1: Register-WMIEvent -Query "Select * from __instanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfDisk_LogicalDisk' AND TargetInstance.NAME='C:' AND TargetInstance.freemegabytes <450000" -Action {ipmo funtions;Send-SMTPmail -smtpserver 'smtp.mail.yahoo.com.br' -from 'juniorlaerte@yahoo.com.br' -to 'laertejuniordba@hotmail.com' -subject 'Event Notification' -body 'Hey..hey..wakeup Man. Take a look in Your Disk C, it is almost full'}

and as you can see, works :

image

But Laerte, how do you know which property to use in WQL ?

Well, in Ravikanth´s EBook you will find a link to a WMI explorer (Set-Alias Pandora´s Box heheh ), a tool to facilitate our day Smile

And if I search for Win32_PerfFormattedData_PerfDisk_LogicalDisk class, I will find all the properties that I can work :

image

(Yes, I drew it and yes, I did not have childhood)

Then this way, it is not so complicated. Now just change in WQL

targetinstance.<property> to  the property that you want.

Well I’m tired and I have to do some remote work now..in the II part I will show how to make this code be scalable.

By By Guys..and BY BY ARGENTINA ……

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL, Virtual Pass BR. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s