首页 > 解决方案 > 如何在我的 PowerShell HTML 报告中添加 2 列?

问题描述

我有 PowerShell 脚本,我写了这段代码:

$computers  = Get-Content D:\Dev\Powershell\Powershell_TXT_FILE\pickup1.txt | Where {
    -not ($_.StartsWith('#'))
} | foreach {
    if (Test-Connection $_ -Quiet -Count 1) {
        New-Object psobject -Property @{
            Server = $_
            Status = "Online"
        }
    } else {
        New-Object PSObject -Property @{
            Server = $_
            Status = "Offline"
        }
    }
}
$computers | ConvertTo-Html -Property Server | Foreach {
    if ($_ -like "*<td>Online</td>*" ) {
        $_ -replace "<tr>","<tr bgcolor=green>"
    } else {
        $_ -replace "<tr>","<tr bgcolor=red>"
    }
} | Out-File D:\Share\Powershell\Powershell_TXT_FILE\test.html

如何在脚本上添加 2 或 4 列。我想得到这个结论:

截屏

标签: powershell

解决方案


您是否希望第 3 列和第 4 列存在但始终为空白?

如果是这样,请尝试:

$computers  = Get-Content D:\Dev\Powershell\Powershell_TXT_FILE\pickup1.txt | Where {
    -not ($_.StartsWith('#'))
} | foreach {
    if (Test-Connection $_ -Quiet -Count 1) {
        New-Object psobject -Property @{
            Server = $_
            Status = "Online"
        }
    } else {
        New-Object PSObject -Property @{
            Server = $_
            Status = "Offline"
        }
    }
}
$computers | ConvertTo-Html -Property Server | Foreach {
    if ($_ -like "*<td>Online</td>*" ) {
        $_ -replace "<tr>","<tr bgcolor=green>"
    } else {
        $_ -replace "<tr>","<tr bgcolor=red>"
} | %{$_ -replace "</td></tr>","</td><td>&nbsp;</td><td>&nbsp;</td></tr>"}
} | Out-File D:\Share\Powershell\Powershell_TXT_FILE\test.html

这会将带有空格的两列添加到表中每一行的末尾。(基于 HTML 表中的值。

现在,如果您不介意标题,更容易,将您想要的数据字段添加到第一行..

$computers  = Get-Content D:\Dev\Powershell\Powershell_TXT_FILE\pickup1.txt  | Where { -not ($_.StartsWith('#')) | select *,xx,yy

其中 XX 和 YY 是您在 HTML 表中添加的两个字段。


推荐阅读