【问题标题】:PowerShell HTML Output not displaying correctlyPowerShell HTML 输出未正确显示
【发布时间】:2013-05-15 23:04:55
【问题描述】:

我有这个输出到 HTML 的 PowerShell 脚本。下面是sn-p的代码。

#Services Check
Write-Host "Services Check"
$html = $html + @"
    <h3>Services Checks</h3>
    <table>
        <tr>
            <th>Name</th>
            <th>Result</th>
        </tr>
"@
get-service -computername server01 -Name *service* |
    foreach-object{
    if($_.Status -ne "Running"){
        Write-Host $_.Name "is not running" -ForegroundColor "red"
        $html = $html + @"
        <tr>
            <td class="name">$_.Name</td>
            <td class="red">is not running</td>
        </tr>
"@
    }else{
        Write-Host $_.Name "is running" -ForegroundColor "green"
        $html = $html + @"
        <tr>
            <td class="name">$_.Name</td>
            <td class="green">is running</td>
        </tr>
"@
    }
}
$html = $html + "</table>"

我的问题出在 PS 控制台输出上,它很好地列出了服务的名称,但在 HTML 输出中,结果如下(表格格式)。

服务检查 System.ServiceProcess.ServiceController.Name 正在运行

我可以做些什么来反对这个不具描述性的列表。

提前致谢

【问题讨论】:

    标签: html powershell scripting


    【解决方案1】:

    尝试写信$():

    $($_.Name)
    

    在字符串(双引号)中,您需要以这种方式强制属性变量扩展。

    【讨论】:

    • +1 如果只是简单的引用对象,它使用toString()方法来提供值,但是当你需要在一个字符串中引用一个对象的属性时,你需要把它包装成像 CB 的回答这样的表达方式
    • @Graimer 感谢您提供更好的说明!
    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 2019-03-31
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多