我想监视远程计算机上的服务列表。这些服务在所有远程计算机上并不相同。
我最接近的方法就是监视所有在远程机器上停止的服务,但我似乎找不到一种方法来编写脚本来监视服务列表。
这是我正在编写的脚本:
$Date = Get-Date -Format dd-MMM-yyyy
$Time = Get-Date -Format "hh:mm:ss tt"
$Style = @"
<!DOCTYPE html>
<html>
...
"@
$ServerList = Get-Content -Path C:\temp\computers1.txt
$body = $ServerList |
Foreach-Object {
Get-Service -ComputerName $_ | Where-Object {
$_.Status -ne "Running" -and
$_.StartType -like "Automatic"
}
} |
Select-Object MachineName, Status, DisplayName, StartType |
Sort-Object -Property MachineNAme -Descending |
ConvertTo-Html
$colorTagTable = @{
Stopped = ' bgcolor="#ff0000">Stopped<';
Running = ' bgcolor="#00ff00">Running<'
}
# get possible values look them in text sorrounded by > < and replace
# them with style (pun intended).
$colorTagTable.Keys | foreach {
$body = $body -replace ">$_<", ($colorTagTable.$_)
}
ConvertTo-Html -Head $Style -Body $body | Out-File "C:\temp\srv.htm" 发布于 2016-07-10 05:52:57
如果有疑问,请阅读documentation。
-ComputerName
获取在指定计算机上运行的服务。默认值为本地计算机。
键入远程计算机的NetBIOS名称、IP地址或完全限定的域名(FQDN)。若要指定本地计算机,请键入计算机名称、点(.)或localhost。
..。
-Name
指定要检索的服务的服务名称。允许使用通配符。默认情况下,此cmdlet获取计算机上的所有服务。
$Style = @"
<style>
...
</style>
"@
$ServiceList = 'NetLogon', 'Spooler', 'W32Time'
$ServerList = Get-Content -Path C:\temp\computers1.txt
Get-Service -ComputerName $ServerList -Name $ServiceList |
Select-Object MachineName, Status, DisplayName, StartType |
Sort-Object -Property MachineNAme -Descending |
ConvertTo-Html -Head $Style |
Out-File 'C:\temp\srv.htm'特定计算机上不存在的服务将被忽略,除非该计算机上没有运行任何服务,在这种情况下,您将得到一个错误。如果您想要忽略它,请使用参数-ErrorAction SilentlyContinue运行Get-Service。
发布于 2016-07-10 15:04:54
对于服务列表,我将有类似的东西,以实际获得服务,这应该是运行的,并有电子邮件报告,而不是记录和检查html页面
$servicelist=Get-WmiObject -Class Win32_Service -Filter "state = 'stopped' and startmode = 'auto'" | select Name
$From = "YourEmail@gmail.com"
$To = "AnotherEmail@YourDomain.com"
$Subject = "Daily Service Report From "
$Body = "get-content C:\temp\srv.htm "
$SMTPServer = "smtp.email.com"
#$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer #-port $SMTPPorthttps://stackoverflow.com/questions/38286432
复制相似问题