出力系(Out-*, Write-*)
出力に関する基本事項のピックアップ&スニペット
Write-Output
出力はパイプラインに送出可能。
出力結果を変数に格納可能。
意識しなくても、Write-Outputするコマンドレットは多い。
配列は改行されて出力される。コンソールもファイルも。
$message = @("This is my long long message.", "How about you?", "I am very fine.") Write-Output $message Write-Output $message > temp.log
コンソール出力
This is my long long message. How about you? I am very fine.
ファイル出力
This is my long long message. How about you? I am very fine.
Write-Host
前景/背景の色指定が可能
コンソール直接出力なので、後で加工することができない。
改行されずに出力されるが、-Separatorで指定は可能。背景色は見ての通り。
$message = @("This is my long long message.", "How about you?", "I am very fine.") Write-Host $message -ForegroundColor Red -BackgroundColor Blue Write-Host $message -ForegroundColor Yellow -BackgroundColor Green -Separator "`r`n"
This is my long long message. How about you? I am very fine. This is my long long message. How about you? I am very fine.
Write-Warning
黄色文字で出力され、先頭に"WARNING: "が出力される。
リダイレクトでなにも出力されなかったので、Write-Host -Foreground Yellow と同じ。
$message = "You must shutdown and sleep now. You work too hard." Write-Warning $message
WARNING: You must shutdown and sleep now. You work too hard.
Write-Error
例外を投げるのは、throw "エラー"のようにthrowで投げる。
出力が例外発生時に似ているが、むしろ例外発生後のエラー出力用と思われる。
see also http://suyamasoft.blue.coocan.jp/PowerShell/Tutorial/index.html#try
function testRaiseError() { throw "testRaiseError" } try { Write-Host "Before testRaiseError()" -ForegroundColor Yellow testRaiseError Write-Host "After testRaiseError()" -ForegroundColor Yellow } catch [System.Net.WebException], [System.IO.IOException] { # Never come this line. This is a sample. } catch { Write-Error -Message $Error[0].Exception.Message -Category InvalidArgument } finally { Write-Host "finally section." }
出力結果を分かりやすくするために、example.ps1にて実行
Before testRaiseError() ・・・\example.ps1 : testRaiseError + CategoryInfo : InvalidArgument: (:) [Write-Error]、WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,example.ps1 finally section.
Write-Progress
プログレスバー的に進捗表示
see also https://www.forsenergy.com/ja-jp/windowspowershellhelp/html/3f2737ad-2984-4e03-ab2f-ecbd14518b3e.htm
for ($i = 1; $i -lt 101; $i++ ) { Start-Sleep -Milliseconds 200 $message = "{0}% Complete:" -F $i Write-Progress -activity "Search in Progress" -status $message -percentcomplete $i }
Search in Progress 48% Complete: [ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ]
Out-GridView
スクリプトらしからぬ、グリッドウィンドウを使ってデータを表示します。
$message = @{ "Key1" = "This is my long long message."; "Key2" = "How about you?"; "Key3" = "I am very fine." } $message | Out-GridView -Title 連想配列の中身
Out-Host
-Paging があるので、more/less的に使える。(Get-Helpには、なぜか使えなかった)
でも、Qで終了すると例外メッセージが出力される...
Get-Process | Out-Host -Paging
PS C:\Users\User01\Documents\PowerShell\Basic> Get-Process | Out-Host -Paging Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 420 19 9660 20744 0.41 440 1 Code 653 132 289244 279864 593.94 3640 1 Code 807 46 39904 77788 39.16 6080 1 Code 221 15 6900 9448 0.23 6528 1 Code 384 39 47500 66644 4.33 8444 1 Code 419 33 24980 54944 126.38 9736 1 Code 321 53 63872 75708 15.17 9828 1 Code 305 15 11320 12624 0.19 6776 1 CodeHelper 133 9 4544 7924 127.61 2404 1 conhost 132 9 4020 6596 0.83 3724 1 conhost 115 8 5448 7192 0.05 10060 1 conhost <Space> 次のページ、<CR> 次の行、Q 終了
Out-File
エンコードは、Unicode(既定値),UTF7,UTF8,UTF32,ASCII,OEM…
UnicodeはUTF-16 LE、UTF8はBOM付き、OEMはSHIFT-JISになる(Windows)。
-Appendで追加モードになります。
-Forceで読み取り専用でも書き込みます。
-NoClobberで既存のファイルを上書きしません。
-Width
Get-Help | Out-File -FilePath "Get-Help_OEM.log" -Encoding OEM Get-Help | Out-File -FilePath "Get-Help_UTF8.log" -Encoding UTF8 Get-Help | Out-File -FilePath "Get-Help_Unicode.log" -Encoding Unicode
Out-Null
なにも出力されない。F8でピンポイント実行しても、ステートメントが出力されない。
> NULL 的な用途。
Get-Process | Out-Null
Out-String
出力されるオブジェクトを文字列配列に変換する場合に使用します。
-Streamを付けると、オブジェクト毎に文字列を送ります。
Get-Alias | Out-String | Select-String "Get-" Get-Alias | Out-String -Stream | Select-String "Get-"