Нечто подобное должно работать.
одной таблицы
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 в примере) с именем компьютера. Отдельные таблицы объединяются и объединяются в один HTML-документ в конце, используя открытый (без ввода) вызов на ConvertTo-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
Styling
Вы найдете 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'. –