Powershell创建简洁的HTML报告例子

支持所有版本

把结果变成复杂的HTML报告,一个简单的方法是定义三个脚本块:一个用作HTML的开头文档,一个用作它的结尾,还有一个是存放动态对象的表格

接着,把这些脚本块传入到ForEach-Object,分别对应脚本的开始块、中间要处理的动态列表块和结束代码块。

下面有个简单的例子阐述如何用它创造一个服务报告:


$path = "$env:temp\report.hta"

 

$beginning = {

 @'

    <html>

    <head>

    <title>Report</title>

    <STYLE type="text/css">

        h1 {font-family:SegoeUI, sans-serif; font-size:20}

        th {font-family:SegoeUI, sans-serif; font-size:15}

        td {font-family:Consolas, sans-serif; font-size:12}

 

    </STYLE>

 

    </head>

    <image src="https://www.nhooo.com/yourlogo.gif" />

    <h1>System Report</h1>

    <table>

    <tr><th>Status</th><th>Name</th></tr>

'@

}

 

$process = {

    $status = $_.Status

    $name = $_.DisplayName

 

    if ($status -eq 'Running')

    {

        '<tr>'

        '<td bgcolor="#00FF00">{0}</td>' -f $status

        '<td bgcolor="#00FF00">{0}</td>' -f $name

        '</tr>'

    }

    else

    {

        '<tr>'

        '<td bgcolor="#FF0000">{0}</td>' -f $status

        '<td bgcolor="#FF0000">{0}</td>' -f $name

        '</tr>'

    }

}

 

 

$end = {

@'

    </table>

    </html>

    </body>

'@

 

 

}

 

 

Get-Service |

  ForEach-Object -Begin $beginning -Process $process -End $end |

  Out-File -FilePath $path -Encoding utf8

 

Invoke-Item -Path $path