共用方式為


Write-Information

指定 Windows PowerShell 如何處理命令的資訊流數據。

語法

Default (預設值)

Write-Information
    [-MessageData] <Object>
    [[-Tags] <String[]>]
    [<CommonParameters>]

Description

Write-Information cmdlet 指定 Windows PowerShell 如何處理命令的信息流數據。

Windows PowerShell 5.0 引入了一個新的結構化資訊流(在 Windows PowerShell 流中編號為 6),可用於在腳本與其調用方(或宿主環境)之間傳輸結構化數據。 Write-Information 允許您向流中添加資訊性消息,並指定 Windows PowerShell 如何處理命令的資訊流數據。 資訊流也適用於 PowerShell.Streams作業、計劃作業和工作流。

備註

信息數據流不會遵循其訊息前面加上 “[Stream Name]:” 的標準慣例。 這是為了簡潔和視覺上的清爽。

$InformationPreference 喜好設定變數值會決定您提供給 Write-Information 的訊息是否顯示在腳本作業的預期點。 由於此變數 SilentlyContinue的預設值為 ,因此預設情況下不會顯示資訊性消息。 如果不想更改 $InformationPreference的值,可以通過向命令添加 InformationAction common 參數來覆蓋其值。 如需詳細資訊,請參閱 about_Preference_Variablesabout_CommonParameters

備註

從 Windows PowerShell 5.0 開始,Write-HostWrite-Information 的包裝器。這允許您使用 Write-Host 將輸出發送到信息流。 這可讓 擷取隱藏 使用 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 命令以查找 Name 值以 'p' 開頭的所有特徵後,您會顯示一條資訊性消息 “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

在此範例中,您將函數中的資訊流重定向到檔 Info.txt,使用代碼 6>。 當您打開 Info.txt 檔時,您會看到文本“Here you go”。

參數

-MessageData

指定您想要在使用者執行文稿或命令時向使用者顯示的參考訊息。 為了獲得最佳結果,請以引號括住參考訊息。 例如,“Test complete”(測試完成)。

參數屬性

類型:Object
預設值:None
支援萬用字元:False
不要顯示:False
別名:味精

參數集

(All)
Position:0
必要:True
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Tags

指定一個簡單的字串,您可以使用它來排序和篩選您已新增至資訊資料流中的訊息Write-Information。 此參數的運作方式與 中的 New-ModuleManifest 參數類似。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:1
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

CommonParameters

此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters

輸入

None

Write-Information 不接受管道輸入。

輸出

InformationRecord