如果服务停止,发送电子邮件通知

使用此脚本在一个或多个服务未运行时获取电子邮件通知。

这里有一些错误,它发送: System.ServiceProcess.ServiceController正在运行

$service=Get-Service "VSS" if ($Service.Status -ne "Running") { $Body ="$service is not running" } else { $Body = "$service is running " } $From = "email" $To = "emails" $SMTPServer = "smtp" $SMTPPort = "587" $Username = "email" $Password = "password" $Body =$body $Subject = "$computer$ status" $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); $smtp.EnableSSL = $false $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.Send($From, $To, $subject, $body); $computer = $env:computername 

如果你的问题是,为什么它返回system.something而不是你的服务名称,这是因为$service不仅仅是服务的名称,它是$service的全部信息,这就是为什么它返回System.ServiceProcess.ServiceController insted VSS 。 请参阅服务名称:

 $service = Get-Service "VSS" if ($Service.Status -ne "Running") { $Body = "$($service.name) is not running" } else { $Body = "$($service.name) is running " }