このようなものは動作するはずです。
単一のテーブル
Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object {
Write-Host "Automatic Services Stopped :" $_
Get-WmiObject Win32_Service -ComputerName $_ -Filter "startmode = 'auto' AND state != 'running'"
} |
Select-Object DisplayName, Name, State, StartMode |
ConvertTo-Html |
Out-File C:\Dv\Report.html
複数のテーブル
このバージョンはConvertTo-Html -Fragment
を使用してテーブルのシーケンスを作成します。各表の前には、コンピューター名のHTMLヘッダー(この例ではh2)が付いています。 ConvertTo-Html
への裸の(入力なし)呼び出しを使用して、個々のテーブルが連結され、最後に単一のHTMLドキュメントにマージされます。
# Generate the content which should be inserted as a set of HTML tables
$preContent = Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object {
$ComputerName = $_
Write-Host "Automatic Services Stopped :" $ComputerName
# A table (and heading) will only be generated for $ComputerName if there are services matching the filter.
Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "startmode = 'auto' AND state != 'running'" |
Select-Object DisplayName, Name, State, StartMode |
ConvertTo-Html -PreContent "<h2>$ComputerName</h2>" -Fragment
}
# Generate the document which holds all of the individual tables.
$htmlDocument = ConvertTo-Html -Head $htmlHead -PreContent $preContent | Out-String
# Because the document has no input object it will have an empty table (<table></table>), this should be removed.
$htmlDocument -replace '<table>\r?\n</table>' | Out-File C:\Dv\Report.html
スタイリング
あなたはこれらによって生成されたHTMLはかなり生であることを見つけることができます。それはCSSを使用することです取り組むためのより良い方法の一つは、ここではHTMLのテーブルがきれいに見える私のフラグメントです:
$HtmlHead = '<style>
body {
background-color: white;
font-family: "Calibri";
}
table {
border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
width: 100%;
}
th {
border-width: 1px;
padding: 5px;
border-style: solid;
border-color: black;
background-color: #98C6F3;
}
td {
border-width: 1px;
padding: 5px;
border-style: solid;
border-color: black;
background-color: White;
}
tr {
text-align: left;
}
</style>'
# Use the Head parameter when calling ConvertTo-Html
... | ConvertTo-Html -Head $HtmlHead | ...
注:フラグメントのパラメータはConvertTo-付属ないときに頭にのみ適用されますHtml。
'ConvertTo-Html'を使用してください。 –