首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的PowerShell HTML报表中添加2列?

如何在我的PowerShell HTML报表中添加2列?
EN

Stack Overflow用户
提问于 2019-03-14 20:08:44
回答 1查看 237关注 0票数 0

我有PowerShell脚本,我写了下面的代码:

代码语言:javascript
复制
$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列。我想得出这样的结论:

EN

回答 1

Stack Overflow用户

发布于 2019-03-15 00:37:20

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

如果是这样,请尝试:

代码语言:javascript
复制
$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表格中的值。

现在,如果您不介意标题,那么在第一行添加您想要的数据字段就更容易了。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55162191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档