Schedule the following PowerShell script to run periodically to receive email alerts on low disk space:
# Schedule with: %windir%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -WindowStyle "Hidden" -File C:\path_to_scripts\freedisk_space_alert.ps1
$minGbThreshold = 10;
$computers = "localhost"
$smtpAddress = "localhost";
$toAddress = "someone@test.com";
$fromAddress = "someone@test.com";
foreach($computer in $computers)
{
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3";
$computer = $computer.toupper();
$deviceID = $disk.DeviceID;
foreach($disk in $disks)
{
$freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824, 2);
if($freeSpaceGB -lt $minGbThreshold)
{
$smtp = New-Object Net.Mail.SmtpClient($smtpAddress)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($toAddress)
$msg.From = $fromAddress
$msg.Subject = "Diskspace below threshold: " + $computer + "\" + $disk.DeviceId
$msg.Body = $computer + "\" + $disk.DeviceId + " " + $freeSpaceGB + "GB Remaining";
$smtp.UseDefaultCredentials = $false;
$cred = New-Object System.Net.NetworkCredential("\smtpuser", "<enter password>"); # Ensure smtpuser is authorised to send emails
$smtp.Credentials = $cred;
$smtp.Send($msg)
}
}
}
This script is slightly modified from the one found here to include credentials:
http://gavindraper.com/2012/09/22/automatic-low-hard-disk-alerts-for-windows-server/
Thanks Gavin!
Also you may need to enable script execution in PowerShell by running the following:
%windir%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe set-executionpolicy remotesigned
Which will still restrict internet downloaded scripts and is better than setting this value to unrestricted
See: http://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts
No comments:
Post a Comment