共用方式為


Write-Information

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

Syntax

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

Description

Cmdlet Write-Information 會指定 PowerShell 如何處理命令的資訊資料流數據。

Windows PowerShell 5.0 引進了新的結構化信息數據流。 您可以使用此資料流,在腳本與其呼叫端或主應用程式之間傳輸結構化數據。 Write-Information 可讓您將參考訊息新增至數據流,並指定 PowerShell 如何處理命令的資訊資料流數據。 信息數據流也適用於 PowerShell.Streams、作業和排程的工作。

注意

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

$InformationPreference喜好設定變數值會決定您提供給 Write-Information 的訊息是否顯示在腳本作業的預期點。 由於此變數的預設值為 SilentlyContinue,因此預設不會顯示參考訊息。 如果您不想變更 的值 $InformationPreference,您可以將通用參數新增 InformationAction 至命令,以覆寫其值。 如需詳細資訊,請參閱 about_Preference_Variablesabout_CommonParameters

注意

從 Windows PowerShell 5.0 開始,這是一Write-Information種包裝函式,Write-Host可讓您用來Write-Host發出資訊數據流的輸出。 這可擷 隱藏 使用 Write-Host 寫入的數據,同時保留回溯相容性。 如需詳細資訊,請參閱 Write-Host

Write-Information 也是 PowerShell 5.x 中支援的工作流程活動。

範例

範例 1:寫入 Get- 結果的資訊

在此範例中,您會在執行 Get-WindowsFeature 命令之後顯示資訊訊息「取得您的功能!」,以尋找名稱值開頭為 『p』 的所有功能。 $InformationPreference因為變數仍然設定為預設值,SilentlyContinue所以您會新增 InformationAction 參數來覆寫$InformationPreference值,並顯示訊息。 此值 InformationAction 為 Continue,這表示您的訊息已顯示,但如果尚未完成,腳本或命令就會繼續。

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!

範例 2︰寫入資訊並加以標記

在此範例中,您會使用 Write-Information 來讓使用者知道使用者在執行目前命令之後必須執行另一個命令。 此範例會在告知性訊息中新增 Instructions 標記。 執行此命令之後,如果您要針對已標記 Instructions 的訊息搜尋資訊資料流,則此處指定的訊息會位於結果之中。

$message = "To filter your results for PowerShell, pipe your results to the Where-Object cmdlet."
Get-WindowsFeature -Name p*
Write-Information -MessageData $message -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.

範例 3︰將資訊寫入檔案

在這裡範例中,您會使用程式代碼 6>將函式中的資訊資料流重新導向至 Info.txt 。 當您開啟檔案時 Info.txt ,您會看到文字「您在這裡」。

function Test-Info
{
    Get-Process P*
    Write-Information "Here you go"
}
Test-Info 6> Info.txt

範例 4:傳遞物件以寫入資訊

在此範例中,您可以使用 Write-Information 從已通過多個管線的對象 Get-Process 輸出寫入前 10 個最高 CPU 使用率進程。

Get-Process | Sort-Object CPU -Descending |
    Select-Object Id, ProcessName, CPU -First 10 |
        Write-Information -InformationAction Continue

@{Id=12692; ProcessName=chrome; CPU=39431.296875}
@{Id=21292; ProcessName=OUTLOOK; CPU=23991.875}
@{Id=10548; ProcessName=CefSharp.BrowserSubprocess; CPU=20546.203125}
@{Id=312848; ProcessName=Taskmgr; CPU=13173.1875}
@{Id=10848; ProcessName=SnapClient; CPU=7014.265625}
@{Id=9760; ProcessName=Receiver; CPU=6792.359375}
@{Id=12040; ProcessName=Teams; CPU=5605.578125}
@{Id=498388; ProcessName=chrome; CPU=3062.453125}
@{Id=6900; ProcessName=chrome; CPU=2546.9375}
@{Id=9044; ProcessName=explorer; CPU=2358.765625}

參數

-MessageData

指定您想要在使用者執行指令碼或命令時顯示的告知性訊息。 為了獲得最佳結果,請以引號括住告知性訊息。

Type:Object
Aliases:Msg, Message
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Tags

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

Type:String[]
Position:1
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

Object

Write-Information 接受傳送至信息數據流的管道物件。

輸出

InformationRecord