This script will check a disk’s free space and send a warning if the server has dropped below the limit you have specified.
The settings are in a XML file. The main entity is the filesystem – let’s say a drive letter or disk. Each disk must be defined as an element, which includes values such as the hostname, limit, and list of recipents.
Both files for this solution must be in the same folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
$ScriptPath = $MyInvocation.MyCommand.Path $RunPath = Split-Path $ScriptPath $Settings = [xml](Get-Content "$RunPath\watchdog.config.xml") $Log = "$RunPath\$($Settings.freeSpaceWatchdog.globalSettings.log)" $SMTPServer = $Settings.freeSpaceWatchdog.globalSettings.smtpserver $EmailSender = $Settings.freeSpaceWatchdog.globalSettings.emailSender $Timestamp = Get-Date -Format "yyyy.MM.dd HH:mm" foreach ($Item in $Settings.freeSpaceWatchdog.filesystem) { $ComputerName = $($Item.Hostname) $Disk = $($Item.Volume) $Limit = [math]::round($($Item.Limit), 2) $EmailRecipients = $($Item.EmailRecipients) $Data = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -Filter "DeviceID='$Disk'" | Select-Object FreeSpace, Size $Size = [math]::round($Data.Size / 1GB, 2) $FreeSpace = [math]::round($Data.FreeSpace / 1GB, 2) if ($FreeSpace -le $Limit) { "$Timestamp - Warning - $Comptername disk $Disk, limit $Limit, freespace $Freespace" >> $Log Send-MailMessage ` -From $EmailSender ` -To $EmailRecipients ` -SmtpServer $SMTPServer ` -Subject "$Computername disk $Disk warning" ` -Body "On $ComputerName disk $Disk has exceeded the limit $Limit GB.`n`nSize $Size GB`nFree space $FreeSpace GB" ` -Priority High } else { "$Timestamp - OK - $Computername disk $Disk, limit $Limit, freespace $Freespace" >> $Log } } |
Here is an overview:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<freeSpaceWatchdog> <globalSettings> <smtpserver>mailer15</smtpserver> <emailSender>freespacelimit@mydomain.com</emailSender> <log>freespacewatchdog.log</log> </globalSettings> <filesystem volume="C:"> <hostname>mbx01</hostname> <limit>50</limit> <emailRecipients>helpdesk@mydomain.com</emailRecipients> </filesystem> <filesystem volume="E:"> <hostname>mbx02</hostname> <limit>70</limit> <emailRecipients>helpdesk@mydomain.com</emailRecipients> </filesystem> </freeSpaceWatchdog> |