gpt4 book ai didi

powershell - 在 Powershell 中使用 HTML 创建表格

转载 作者:行者123 更新时间:2023-12-03 23:19:39 25 4
gpt4 key购买 nike

我想知道服务停止了哪些设置为自动和输出文件转到 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
}

最佳答案

像这样的事情应该有效。

单 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

多 table

此版本使用 ConvertTo-Html -Fragment 创建一系列表.每个表前面都有一个带有计算机名称的 HTML header (在示例中为 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 | ...

注意:Head 仅在 Fragment 参数为 时适用不是 随 ConvertTo-Html 提供。

关于powershell - 在 Powershell 中使用 HTML 创建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38991984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com