html - Powershell将流程列表转换为HTML

标签 html powershell

将名称和前10个服务的状态导出到一个html文件中,并用运行绿色和停止红色为这些行上色。

这是任务。

我尝试过了:

Get-Service | Select-Object -First 10 -Property Name,Status
Get-Service | Select-Object -First 10 -Property Name,Status | ConvertTo-Html  > first.txt

(Get-Content hallo.txt) -replace '<td>','<td style="color:#00ff00">'|Set-Content final.html


但是现在我有一个问题,就是一切都是绿色的。
我如何区分已停止和正在运行?

我认为我一直想提出的这个想法行不通。
在将过程导出到文件之前,也许可以区分一下?但是我不知道该怎么做。

最佳答案

如果我们坚持文字替换的主题,则可以执行以下操作:

$html = Get-Service | Select-Object -First 10 -property Name,Status |
    ConvertTo-Html | Foreach-Object {
        if ($_ -match '<td>Stopped</td>') {
            $_ -replace '<td>','<td style="color:red">'
        }
        elseif ($_ -match '<td>Running</td>') {
            $_ -replace '<td>','<td style="color:green">'
        }
        else {
            $_ 
        }
    }
$html | Set-Content final.html

关于html - Powershell将流程列表转换为HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59545053/

相关文章:

html - Div重叠

javascript - 是否可以使用代码来转换格式为 'list' : "translation"? 的列表

html - html 中的表单 - 如何让表单做两件事?

JavaScript:点击提交给自己后清除表单数据

powershell - 在PowerShell中解析数​​千个小文件的最快方法

powershell - 检查输入 blob 是否存在

html - Dompdf 不遵循 css 的 div 错误

powershell - Select-Object返回第一个结果作为我的列名,而不是结果

powershell - 在 PowerShell 中创建列表框

powershell - 在将 Powershell 命令发送到管道之前是否可以获取其完整输出?