系統管理員監測系統事件日誌,如果DHCP服務器出現五天之內的錯誤信息,則將錯誤信息發送到指定管理員的郵箱中。
$body=$null
$dhcplogs=$null
$servicename="dhcpserver"
$smtpsrv="your smtpserver"
$emailfrom="this mail from"
$emailto="your e-mail"
$subject="DHCP 錯誤信息"
$dhcpservice=get-service|?{$_.name -eq $servicename}
if($dhcpservice)
{
$dhcplogs=get-eventlog system -source -after ([system.datetime]::now).adddays("-5") -entrytype error
foreach($log in $dhcplogs)
{
$body+=$log| ft -wrap|out-string
}
$smtp=new-object net.mail.smtpclient($smtpsrv)
$smtp.send($emailfrom,$emailto,$subject,$body)
}