伺服器硬碟空間小於百分之10的情況下,自動化的發出警告mail

伺服器硬碟空間小於百分之10的情況下,自動化的發出警告mail

透過下面的PowerShell指令

設定為只要有硬碟空間小於百分之10的情況下,以SMTP Server寄信通知收件者

指令如下

$mythreshold = 10

$freespace = gwmi Win32_LogicalDisk -Filter "DriveType=3" | select Name, FileSystem,FreeSpace,BlockSize,Size

ForEach($item in $freespace)

{

$item.BlockSize=(($item.FreeSpace)/($item.Size))*100

$item.FreeSpace=($item.FreeSpace/1GB)

$item.Size=($item.Size/1GB)

if ($item.BlockSize -lt $mythreshold){

$from = "ithelp@athentek.com"

$to = "ithelp@miniasp.com"

$subject = "Low Disk Space!"

$body = "Free Space Remaining: " + "<p></p>磁碟總容量(GB):" + $item.size + "<p></p>剩餘空間(GB):" + $item.FreeSpace + "<p></p>磁區:" + $item.name + "<p></p>剩餘百分比:" + $item.BlockSize +"%"

$smtpServer = "192.168.2.3"

Send-MailMessage -From $from -to $to -Subject $subject -Body $body -SmtpServer $smtpServer -BodyAsHtml -Encoding UTF8

}

}

參考文件:http://itservice.miniasp.com/post/2014/10/03/%E4%BC%BA%E6%9C%8D%E5%99%A8%E7%B6%AD%E9%81%8B-%E7%A1%AC%E7%A2%9F%E7%A9%BA%E9%96%93%E6%AA%A2%E8%A6%96.aspx