Getting an notification by Email when disk is full?

Turbo20VT

Reputable
Aug 25, 2015
25
0
4,530
Hello,

I would like to get an emailmessage, when my SSD is full. Is needs to send automaticly an email, my problem now is, I wanna set it below 1GB, so lower dan 1GB it needs to send an email.

I have this code, it send's an email, but only when I activate it in te taskscheduwler.

$smtpserver="192.168.1.5"
$sender="myemail@email.nl"
$receiver="myemail@email.nl"
$subject="Diskspace is full!"
$message="Diskspace is full $env:computername !

$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" |
Select-Object Size,FreeSpace

$result = $disk.FreeSpace / 1GB

if ($result -lt 5) {
Send-MailMessage -smtpserver $smtpserver -From $sender -To $receiver -Subject $subject -Body $message
quit
}
else {
quit
}

 
When you activate it in Task Scheduler?

How often do you want it checking your disk space?

If you only need it checking once or twice per day, you could schedule that as a Weekly task to run Sun-Sat at whatever time works best.

If you need it constantly checking throughout the day, you could create a scheduled task that launches it on startup and add a while loop to your code to make it constantly checking the drive.

That would look something like this:

$smtpserver="192.168.1.5"
$sender="myemail@email.nl"
$receiver="myemail@email.nl"
$subject="Diskspace is full!"
$message="Diskspace is full $env:computername !


while(true) {
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" |
Select-Object Size,FreeSpace

$result = $disk.FreeSpace / 1GB

if ($result -lt 5) {
Send-MailMessage -smtpserver $smtpserver -From $sender -To $receiver -Subject $subject -Body $message
quit
}
Start-Sleep -s 10
}

There's two changes worth noting in the code:


    I removed the else event. We don't want the loop to exit if your storage isn't breaking the boundaries. I left the exit in the event the storage is less than your specified amount. This is so it doesn't constantly send emails once you drop below 5gb or 1gb or whatever.


    I added Start-Sleep -s 10. This causes the script to sleep for 10 seconds between each time it checks your remaining storage. The purpose of this is so that you don't end up slowing your computer to a crawl by constantly querying the hard drive for storage space.
 
I think the core of the question was automated notifications when a drive reaches a certain point. That's not unreasonable. I do that at work. (admittedly, not with a 1gb limit though)

Also, if you are okay with C#, here's a solution I just threw together. Haven't tested it too much; but it'll do the trick, I'm sure.

http://pastebin.com/X5aQwwYD