支持所有版本
把結果變成復雜的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.jb51.net/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
新聞熱點
疑難解答