在 Powershell 中使用 HTML 创建 table

Create table using HTML in Powershell

我想知道服务已停止,哪些服务设置为自动,输出文件转到 HTML 页面。在 HTML 输出中,我想在 table 服务器名称之上为已停止的服务创建 table。谁能告诉我..

$ServerListFile = "C:\Dv\Server_List.txt" 
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
foreach ($Computername in $ServerList) {
Write-Host "Automatic Services Stopped :" $Computername 
Get-wmiobject win32_service -computername $Computername  -Filter  "startmode  
= 'auto'  AND state != 'running'" | Select DisplayName,Name,State,startmode  
|  Format-Table  -auto | Out-File C:\Dv\Report.html
}

像这样的东西应该有用。

单身table

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

多个tables

此版本使用 ConvertTo-Html -Fragment 创建了一个 table 序列。每个 table 前面都有 HTML header(示例中的 h2)和计算机名称。使用对 ConvertTo-Html.

的裸(无输入)调用,最后将单个 table 连接并合并到单个 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 table 看起来更漂亮:

$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 | ...

注意:仅当 Fragment 参数 而不是 随 ConvertTo-Html 一起提供时,Head 才适用。