Alert yourself, if your System is running out of space

You all know how it feels to run out of space 😉 Well it is not very fun and also not very zen…
But installing a heavily oversized monitoring solution for every little test server is also not the zen way. So why not create a little script, alerting you over Telegram telling you how much space is left.

First you have to create a Telegram bot, if you do not know how to do it, here you can find a description –> How to create a Telegram bot

As a start you can use the following up script, you just need to change the variables at the top of the script.

#Telegram Definitions
$MyToken = "XXXX"
$chatID = XXXX
#Drive Definitions
$Space_low = "10000000000" # Minimum space to be left 10Gb => 10000000000 Byte
$Driveletter = "C:"
$Freespace = get-WmiObject win32_logicaldisk -Filter "DeviceID='$Driveletter'" | Foreach-Object {$_.FreeSpace}
$FreespaceGB = $Freespace/1024/1024/1024
$FreespaceGB = “{0:N2}” –f $FreespaceGB
#write-host $Freespace
if ($Freespace -lt $Space_low) { 
    
    Invoke-RestMethod -Uri "https://api.telegram.org/bot$($MyToken)/sendMessage?chat_id=$($chatID)&text=$("Not much space left on $Driveletter !!! You have only $FreespaceGB GB left")"
    
   }
else {
 Invoke-RestMethod -Uri "https://api.telegram.org/bot$($MyToken)/sendMessage?chat_id=$($chatID)&text=$("Enough space left on $Driveletter . You have $FreespaceGB GB left")"
}

Then add the script to the windows task scheduler and let it run in the frequency you want to have the drive checked. Now you don’t need to fear to run out of space ever again.

Leave a Reply

Powered by WordPress.com.

Up ↑

%d bloggers like this: