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

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

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

这里出了问题,它发送: 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

答案1

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

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

相关内容