【发布时间】:2017-05-08 10:05:19
【问题描述】:
我正在开发一个简单的解决方案,如果我的远程服务器上的任何服务停止,它需要发送电子邮件警报。
以下是流程:
- 在记事本文件中写入我要监控的所有服务
- PowerShell 脚本将从文件中读取所有服务并检查其状态。
- 如果任何服务停止,它将向我的 HTML 格式添加内容并发送电子邮件。
一切正常,但我得到的是System.ServiceProcess.ServiceController,而不是实际的服务名称。我在 PowerShell 控制台上打印东西,并在那里获得服务名称。
代码:
$Services = Get-Content D:\Services.txt
foreach ($Service in $Services) {
Write-Host "Service Name: "$Service
}
当我执行这个时,我得到了预期的服务名称。
if ($Service.Status -eq "Stopped") {
Write-Host "Service is stopped"
$dataRow = "<tr><td width='10%'>199.199.50.512</td><td width='10%' >$Service</td><td width='10%' align='center'>Stopped</td></tr>"
Add-Content $ServiceReport $dataRow;
}
这里写了$Service,我期待我的服务名称,但我在HTML报告中得到System.ServiceProcess.ServiceController。
HTML 报告:
【问题讨论】:
标签: powershell service