在 html 报告中为多个服务仅显示一次服务器名称

Display servername only once for multiple services in html report

下面是我的代码,它在 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

我需要的报告是只显示一次服务器名称,如下所示


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

请对此有所了解。

这是传递给 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