powershell - 如何在我的 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 列。我想得到这个结论:
解决方案
您是否希望第 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> </td><td> </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 表中添加的两个字段。
推荐阅读
- python - Pandas 绘图条顺序类别
- sql - Oracle DB 中复杂的 SELECT 语句
- html - 如何防止此导航栏崩溃?
- laravel - 如何在 Laravel 5.4 中组合模型和按模型排序两个数据
- reactjs - Webpack+SemanticUI+React:进程未定义
- python - 在散列中使用 .digest()?
- c++ - c ++ CreateThread非静态成员没有特定成员名称
- php - 具有非原始类型提示参数的 Laravel API 端点
- excel - VBA range.formula 问题(LEN、RIGHT 和 LEFT)
- python - 无法在 python 脚本中运行 os.popen