在 Powershell 中使用 HTML 创建表格

Siv*_*iva 5 powershell powershell-2.0

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

$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
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*ent 4

像这样的东西应该有效。

单桌

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
Run Code Online (Sandbox Code Playgroud)

多桌

此版本使用创建一系列表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
Run Code Online (Sandbox Code Playgroud)

造型

您会发现这些生成的 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 | ...
Run Code Online (Sandbox Code Playgroud)

注意:只有当 ConvertTo-Html未提供Fragment 参数时,Head 才适用。