$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
}
}