AvoidUsingWriteHost

重大度レベル: 警告

形容

このルールは、Show動詞を使わない関数におけるWrite-Hostの使用を検出します。 Write-Host ホスト内で表示専用の出力を生成するよう設計されており、色付きのテキストを印刷したり、 Read-Hostでユーザーに入力を促したりします。 出力は ToString() メソッドを用い、結果はPowerShellホストプログラムによって異なります。

Write-Hostはパイプラインに出力を送らないため、パイプラインにデータを渡すにはWrite-Outputまたは暗黙の出力が必要です。

関数で Write-Host を使わないでください。ただし、 Show 動詞は 明確にユーザーに情報を表示することを意味します。 このルールは、Show 動詞を持つ関数には適用されません。

意図に応じて、 Write-HostWrite-Output または Write-Verbose に置き換えてください。 ログ Write-VerboseWrite-Output をオブジェクト返却に使いましょう。

非準拠

function Get-MeaningOfLife
{
    Write-Host 'Computing the answer to the ultimate question of life, the universe and everything'
    Write-Host 42
}

対応

情報メッセージには Write-Verbose を使用します。 ユーザーは、Verbose パラメーターを指定して、メッセージを表示するかどうかを決定できます。

function Get-MeaningOfLife {
    [CmdletBinding()]Param() # makes it possible to support Verbose output

    Write-Verbose 'Computing the answer to the ultimate question of life, the universe and everything'
    Write-Output 42
}

function Show-Something {
    Write-Host 'Show something on screen'
}