在 PowerShell 中解釋 HTML 格式化?
HTML 是 PowerShell 中另一種輸出形式。它是豐富的輸出形式,您可以使用各種 CSS 樣式使輸出更具互動性。我們將使用 ConvertTo-HTML cmdlet 將輸出轉換為 HTML 格式。
以下是 **ConvertTo-HTML** cmdlet 的語法。
示例
ConvertTo-Html [-InputObject <PSObject>] [[-Property] <Object[]>] [[-Body] <String[]>] [[-Head] <String[]>] [[-Title] <String>] [-As <String>] [-CssUri <Uri>] [-PostContent <String[]>] [-PreContent <String[]>] [-Meta <Hashtable>] [-Charset <String>] [-Transitional] [<CommonParameters>] ConvertTo-Html [-InputObject <PSObject>] [[-Property] <Object[]>] [-As <String>] [-Fragment] [-PostContent <String[]>] [-PreContent <String[]>] [<CommonParameters>]
讓我們來看一個使用 InputObject 引數的 ConvertTo-HTML 的簡單示例。
ConvertTo-Html -InputObject (Get-Date)
輸出將在同一個控制檯上。
PS C:\WINDOWS\system32> ConvertTo-Html -InputObject (Get-Date) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML TABLE<title> </head><body> <table> <colgroup><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/></colgroup> <tr><th>DisplayHint</th><th>DateTime</th><th>Date</th><th>Day</th><th>DayOfWeek</th><th>DayOfYear</th><th>Hour</th><th>Kind<th><th>Millisecond</th>< th>Minute</th><th>Month</th><th>Second</th><th>Ticks</th><th>TimeOfDay</th><th>Year</th></tr> <tr><td>DateTime</td><td>17 June 2020 08:33:06</td><td>17-06-2020 00:00:00</td><td>17</td><td>Wednesday</td><td>169</td><td>8</td><td>Local</td><td>8 29</td><td>33</td><td>6</td><td>6</td><td>637279795868299280</td><td>08:33:06.8299280</td><td>2020</td></tr> </table> </body></html>
因此,您需要將輸出重定向到 **.html** 副檔名。
ConvertTo-Html -InputObject (Get-Date) > C:\temp\dateoutput.html
輸出
在這裡,您也可以使用管道作為 Input Object。下面顯示一個示例。
下面我們將把 **Get-Service** 輸出轉換為 HTML 格式。
Get-Service | Select Name, Status, StartType | ConvertTo-Html | Out-File Servicesoutput.html
當您在名為 **ServicesOutput.html** 的檔案中檢視輸出時,HTML 檔案將如下所示。
輸出
您也可以右鍵單擊並使用記事本、Notepad++ 或任何相容的編輯器進行編輯來檢查 HTML 內容。
HTML 檔案在編輯器中將如下所示。您可以看到標題“**HTML 表格**”,它是自動新增的。您可以在編輯器中更改標題,但這沒有用,因為它每次執行指令碼時都會被覆蓋為預設值。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML TABLE</title> </head><body> <table> <colgroup><col/><col/><col/></colgroup> <tr><th>Name</th><th>Status</th><th>StartType</th></tr> <tr><td>AarSvc_69f5c</td><td>Stopped</td><td>Manual</td></tr> <tr><td>AdobeARMservice</td><td>Running</td><td>Automatic</td></tr> <tr><td>AdobeFlashPlayerUpdateSvc</td><td>Stopped</td><td>Manual</td></tr> <tr><td>AJRouter</td><td>Stopped</td><td>Manual</td></tr> <tr><td>ALG</td><td>Stopped</td><td>Manual</td></tr> <tr><td>AppIDSvc</td><td>Stopped</td><td>Manual</td></tr> <tr><td>Appinfo</td><td>Running</td<td>Manual</td></tr> <tr><td>AppMgmt</td><td>Stopped</td><td>Manual</td></tr> <tr><td>AppReadiness</td><td>Stopped</td><td>Manual</td></tr> <tr><td>AppVClient</td><td>Stopped</td><td>Disabled</td></tr>
因此,要在指令碼中新增標題,您需要使用 –Title 引數。例如:
Get-Service | Select Name, Status, StartType | ConvertTo-Html -Title "Services Output" | Out-File Servicesoutput.html
當您檢視輸出時,您將看到標題。
在編輯器中檢視上述輸出的 HTML 檔案,您現在可以看到新標題。
輸出
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Services Output</title> </head><body> <table>
除了在 **Get-Services** 輸出中使用管道屬性外,您還可以使用 **ConvertTo-HTML** 屬性從之前的命令中選擇輸出。
例如:
Get-Service | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" | Out-File Servicesoutput.html
它將產生與上述 HTML 輸出相同的結果。
如果您需要在結果之前新增描述,則需要使用 –PreContent 引數。例如:
Get-Service | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" -PreContent "Services Output" | Out-File Servicesoutput.html
輸出
您可以使用 HTML 標題來調整 precontent。例如:
Get-Service | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" -PreContent "<h1><center>Services Output</center></h1>" | Out-File Servicesoutput.html
輸出
在上面的示例中,我們為 PreContent 設定了標題和居中對齊。同樣,您可以使用 PostContent 在底部顯示標題。例如:
示例
Get-Service | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" -PreContent "<h1><center>Services Output</center></h1>" -PostContent "<div align=right><b>From: PowerShell Scripter</b></div>" | Out-File Servicesoutput.html