下面是我的代码,它在html文件中给出服务状态
$today=(Get-Date -format dd-MM-yyyy)
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$file_path = $ScriptDir + "\report.html"
$ReportTitle="service Status $today"
$serverlist=Get-Content -Path $ScriptDir + "\serverlist.txt"
$servicelist=Get-Content -Path $ScriptDir + "\serverlist.txt"
$Style = @"
<style>
BODY{font-family:Calibri;font-size:12pt;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;color:black;background-color:#0BC68D;text-align:center;}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;text-align:center;}
</style>
"@
$array = @()
foreach($server in $serverlist) {
Foreach($service in $servicelist) {
$svc = Get-Service $service -ComputerName $server -ea "0"
$obj = New-Object psobject -Property @{
ServerName = $server
DisplayName=$svc.displayname
Name = $svc.name
Status = $svc.status
}
$array += $obj
}
}
$array | Select Computername,displayname,name,status | ConvertTo-Html -property 'ServerName','Displayname','Name','Status' -head $Style -body "<h1> $ReportTitle </h1>" | foreach {if($_ -like "*<td>Running</td>*"){$_ -replace "<tr>", "<tr bgcolor=#089437>"} elseif($_ -like "*<td>Stopped</td>*" -or "*<td>Stopping</td>*" -or "*<td>Pending</td>*" -or "*<td>Starting</td>*"){$_ -replace "<tr>", "<tr bgcolor=#C60B1C>"} else{$_}} |out-file $file_path 现在,我得到的输出类似于下面的html
ServerName Displayname Name Status
WIN0333.infores.com Print Spooler Spooler Running
WIN0333.infores.com Remote Procedure Call (RPC) RpcSs Running
WIN0333.infores.com Windows Time W32Time Running
WIN0333.infores.com Windows Installer msiserver Stopped
WIN0444.infores.com Print Spooler Spooler Running
WIN0444.infores.com Remote Procedure Call (RPC) RpcSs Running
WIN0111.infores.com Print Spooler Spooler Running
WIN0111.infores.com Remote Procedure Call (RPC) RpcSs Running
WIN0111.infores.com Windows Time W32Time Running
WIN0111.infores.com Windows Installer msiserver Stopped我需要的报告是只显示servername一次,如下所示
WIN0333.infores.com Print Spooler Spooler Running
Remote Procedure Call (RPC) RpcSs Running
Windows Time W32Time Running
Windows Installer msiserver Stopped
WIN0444.infores.com Print Spooler Spooler Running
Remote Procedure Call (RPC) RpcSs Running
WIN0111.infores.com Print Spooler Spooler Running
Remote Procedure Call (RPC) RpcSs Running
Windows Time W32Time Running
Windows Installer msiserver Stopped请对此有所了解。
发布于 2021-04-30 00:46:56
这是传递给ConvertTo-Html cmdlet的内容问题。现在传递的每个对象都有计算机名,因此,如果您只想在每台计算机的第一条记录上显示计算机名,则需要按计算机分隔结果,并且只传递每个分组的第一条记录的计算机名。要做到这一点,最简单的方法是使用Group-Object cmdlet,如下所示:
$array | Select Computername,displayname,name,status | Group-Object Computername |
ForEach-Object {
$_.Group[0] #Pass the first record intact
$_.Group | Select displayname,name,status -Skip 1 #Pass all records but the first with no Computername
} | ConvertTo-Html -property 'ServerName','Displayname','Name','Status' -head $Style -body "<h1> $ReportTitle </h1>" | foreach {if($_ -like "*<td>Running</td>*"){$_ -replace "<tr>", "<tr bgcolor=#089437>"} elseif($_ -like "*<td>Stopped</td>*" -or "*<td>Stopping</td>*" -or "*<td>Pending</td>*" -or "*<td>Starting</td>*"){$_ -replace "<tr>", "<tr bgcolor=#C60B1C>"} else{$_}} |out-file $file_path https://stackoverflow.com/questions/67321171
复制相似问题