Write-Information
Windows PowerShell でコマンドの情報ストリーム データを処理する方法を指定します。
構文
Write-Information
[-MessageData] <Object>
[[-Tags] <String[]>]
[<CommonParameters>]
説明
Write-Information
コマンドレットは、Windows PowerShell がコマンドの情報ストリーム データを処理する方法を指定します。
Windows PowerShell 5.0 には、スクリプトとその呼び出し元 (またはホスティング環境) の間で構造化データを送信するために使用できる、新しい構造化情報ストリーム (Windows PowerShell ストリームの番号 6) が導入されています。
Write-Information
では、ストリームに情報メッセージを追加し、Windows PowerShell でコマンドの情報ストリーム データを処理する方法を指定できます。 情報ストリームは、PowerShell.Streams
、ジョブ、スケジュールされたジョブ、ワークフローでも機能します。
手記
情報ストリームは、メッセージの先頭に "[Stream Name]:" を付けることを標準の規則に従っていません。 これは簡潔で視覚的な清潔さを目的としていました。
$InformationPreference
基本設定変数の値は、スクリプトの操作で、Write-Information
に指定したメッセージが予想されるポイントに表示されるかどうかを決定します。
この変数の既定値は SilentlyContinue
であるため、既定では情報メッセージは表示されません。
$InformationPreference
の値を変更しない場合は、InformationAction
共通パラメーターをコマンドに追加することで、その値をオーバーライドできます。
詳細については、「about_Preference_Variables と about_CommonParameters」を参照してください。
手記
Windows PowerShell 5.0 以降では、Write-Host
は Write-Information
のラッパーです。これにより、Write-Host
を使用して情報ストリームに出力を出力できます。
これにより、下位互換性を維持しながら、 を使用して書き込まれたデータの または Write-Host
をキャプチャできます。
詳細については、「書き込みホスト
Write-Information
は、サポートされているワークフロー アクティビティでもあります。
例
例 1: Get- results の情報を書き込む
Get-WindowsFeature -Name p*; Write-Information -MessageData "Got your features!" -InformationAction Continue
Display Name Name Install State
------------ ---- -------------
[ ] Print and Document Services Print-Services Available
[ ] Print Server Print-Server Available
[ ] Distributed Scan Server Print-Scan-Server Available
[ ] Internet Printing Print-Internet Available
[ ] LPD Service Print-LPD-Service Available
[ ] Peer Name Resolution Protocol PNRP Available
[X] Windows PowerShell PowerShellRoot Installed
[X] Windows PowerShell 5.0 PowerShell Installed
[ ] Windows PowerShell 2.0 Engine PowerShell-V2 Removed
[X] Windows PowerShell ISE PowerShell-ISE Installed
Got your features!
この例では、Get-WindowsFeature
コマンドを実行して、'p' で始まる Name 値を持つすべての機能を検索した後、情報メッセージ "Got your features!" を表示します。
$InformationPreference
変数は引き続き既定値に設定されているため、SilentlyContinue
、InformationAction
パラメーターを追加して $InformationPreference
値をオーバーライドし、メッセージを表示します。
InformationAction
値は Continue です。つまり、メッセージが表示されますが、スクリプトまたはコマンドがまだ完了していない場合は続行されます。
例 2: 情報を書き込んでタグ付けする
Get-WindowsFeature -Name p*; Write-Information -MessageData "To filter your results for PowerShell, pipe your results to the Where-Object cmdlet." -Tags "Instructions" -InformationAction Continue
Display Name Name Install State
------------ ---- -------------
[ ] Print and Document Services Print-Services Available
[ ] Print Server Print-Server Available
[ ] Distributed Scan Server Print-Scan-Server Available
[ ] Internet Printing Print-Internet Available
[ ] LPD Service Print-LPD-Service Available
[ ] Peer Name Resolution Protocol PNRP Available
[X] Windows PowerShell PowerShellRoot Installed
[X] Windows PowerShell 5.0 PowerShell Installed
[ ] Windows PowerShell 2.0 Engine PowerShell-V2 Removed
[X] Windows PowerShell ISE PowerShell-ISE Installed
To filter your results for PowerShell, pipe your results to the Where-Object cmdlet.
この例では、Write-Information
を使用して、現在のコマンドの実行が完了した後に別のコマンドを実行する必要があることをユーザーに通知します。
この例では、情報メッセージにタグ Instructions を追加します。
このコマンドを実行した後、情報ストリームで Instructions というタグが付いたメッセージを検索すると、ここで指定したメッセージが結果の中に含まれます。
例 3: ファイルに情報を書き込む
function Test-Info
{
Get-Process P*
Write-Information "Here you go"
}
Test-Info 6> Info.txt
この例では、コード 6>を使用して、関数内の情報ストリームをファイル (Info.txt) にリダイレクトします。 Info.txt ファイルを開くと、"Here you go" というテキストが表示されます。
パラメーター
-MessageData
スクリプトまたはコマンドを実行するときにユーザーに表示する情報メッセージを指定します。 最良の結果を得るには、情報メッセージを引用符で囲みます。 例として、"テスト完了" があります。
型: | Object |
Aliases: | Msg |
配置: | 0 |
規定値: | None |
必須: | True |
パイプライン入力を受け取る: | False |
ワイルドカード文字を受け取る: | False |
-Tags
Write-Information
を使用して情報ストリームに追加したメッセージの並べ替えとフィルター処理に使用できる単純な文字列を指定します。
このパラメーターは、の New-ModuleManifest
パラメーターと同様に機能します。
型: | String[] |
配置: | 1 |
規定値: | None |
必須: | False |
パイプライン入力を受け取る: | False |
ワイルドカード文字を受け取る: | False |
入力
None
Write-Information
はパイプ入力を受け入れません。