オブジェクトをHTMLに変換する(convert-html)

※「convert-html」はPowerShell 1.0では「ConvertTo-Html」という名前に変更されています。

「convert-html」:オブジェクトをHTMLに変換する

構文

convert-html [[-Property] object[]] [-InputObject MshObject] [-Body bodyText] [-Head headText] [-Title titleText]

文字列をIEに出力する(out-ie) - PowerShell Memoで紹介した「out-ie」と組み合わせると便利です。

「System.DateTime」をHTMLテーブル表示
$ie = convert-html -InputObject (get-date) | out-ie
  • $ieIEオブジェクトを取得しておくと、$ie.quit()でIEを終了できます。
「wで始まるプロセスの情報」をHTMLテーブル表示
$ie = ps w* | convert-html -Title "W* Process"| out-ie
  • 「-Title」でタイトルを指定できます。
  • 「System.Diagnostics.Process」の情報をHTMLテーブル表示します。
「wで始まるプロセスのオブジェクト配列」をHTMLテーブル表示
$ie = convert-html -inputobject (ps w*) | out-ie
  • 「System.Object[]」の情報をHTMLテーブル表示します。

テーブルにボーダーを付ける

function set-border()
{
    param([String]$table)
    $table = $table -replace "<table>","<table border='1'>"
    $table = $table -replace "<td></td>","<td><br></td>" 
    $table
}
$ie = ps | convert-html | foreach {set-border($_)} | out-ie
  • TDタグの間にBRタグを入れることで、値が空のセルもボーダーを潰さないようにしています。