在html报告中仅为多个服务显示一次servername
下面是我在html文件中给出服务状态的代码在html报告中仅为多个服务显示一次servername,html,powershell,report,Html,Powershell,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 $Sc
$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
我需要的报告是只显示一次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
请对此有所了解。这是传递给
转换为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
$array |选择Computername、displayname、name、status |组对象Computername |
ForEach对象{
$\组[0]\完整地传递第一条记录
$35; Group |选择displayname、name、status-跳过1#传递所有记录,但第一个记录不包含Computername
}|转换为Html-属性'ServerName'、'Displayname'、'Name'、'Status'-head$Style-body“$ReportTitle”| foreach{if($类似于“*Running*”){$$替换“,”}elseif($类似于“*Stopped*”-或“*Stopping*”-或“*Pending*”-或“*Starting*”){$$替换“,”}否则{$}退出文件$file\u路径
$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