Write-Information

規定 PowerShell 如何處理指令的資訊串流資料。

語法

Default (預設值)

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

Description

該 Write-Information cmdlet 指定了 PowerShell 如何處理指令的資訊串流資料。

Windows PowerShell 5.0 引入了一套新的結構化資訊流。 你可以利用這條串流在腳本與呼叫者或主機應用程式之間傳送結構化資料。 Write-Information 讓你可以為串流新增資訊訊息,並指定 PowerShell 如何處理指令的資訊串流資料。 資訊流也適用於 PowerShell.Streams、 工作及排程任務。

Note

資訊串流不遵循標準慣例,即在訊息前加上「[串流名稱]:」。 此設計旨在簡潔且視覺乾淨。

$InformationPreference偏好變數值決定你提供的Write-Information訊息是否在腳本操作的預期點顯示。 由於此變數的預設值為 SilentlyContinue,預設情況下不會顯示資訊訊息。 如果你不想更改 的 $InformationPreference值,可以透過在指令中加入 InformationAction 的通用參數來覆寫其值。 更多資訊請參見 about_Preference_Variables 與 about_CommonParameters。

Note

從 Windows PowerShell 5.0 開始,Write-Host 是 Write-Information 的包裝器,這讓你可以使用 Write-Host 將輸出輸出到資訊流。 這使得在保留向下相容性的情況下,能夠擷取或抑制所寫 Write-Host 入的資料。 更多資訊請參閱 寫入主機

範例

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

在這個範例中,你會顯示一個資訊訊息「以 'P' 開頭的程序」,然後執行Get-Process尋找所有以 'p' 開頭的程序名稱的指令。 因為變 $InformationPreference 數仍設定為預設 SilentlyContinue,你加入 InformationAction 參數來覆寫該 $InformationPreference 值並顯示訊息。 InformationAction 的值是 Continue,表示你的訊息已經顯示,但腳本或指令會繼續,如果還沒完成的話。

Write-Information -MessageData "Processes starting with 'P'" -InformationAction Continue
Get-Process -Name p*
Processes starting with 'P'

     18    19.76      15.16       0.00    6232   0 PFERemediation
     20     8.92      25.15       0.00   24944   0 policyHost
      9     1.77       7.64       0.00    1780   0 powercfg
     10    26.67      32.18       0.00    7028   0 powercfg
      8    26.55      31.59       0.00   13600   0 powercfg
      9     1.66       7.55       0.00   22620   0 powercfg
     21     6.17       4.54     202.20   12536   1 PowerMgr
     42    84.26      12.71   2,488.84   20588   1 powershell
     27    47.07      45.38       2.05   25988   1 powershell
     27    24.45       5.31       0.00   12364   0 PresentationFontCache
     92   112.04      13.36      82.30   13176   1 pwsh
    106   163.73      93.21     302.25   14620   1 pwsh
    227   764.01      92.16   1,757.22   25328   1 pwsh

範例二:寫入資訊並標註

在這個例子中,你用來 Write-Information 通知使用者在執行完當前指令後,還需要執行另一個指令。 範例中將標籤 "Instructions" 加入資訊訊息。 執行此指令後,當你在資訊流中搜尋標記 "Instructions"為 的訊息時,該訊息會出現在結果中。

$message = "To filter your results for PowerShell, pipe your results to the Where-Object cmdlet."
Get-Process -Name p*
Write-Information -MessageData $message -Tags "Instructions" -InformationAction Continue
 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     18    19.76      15.16       0.00    6232   0 PFERemediation
     20     8.92      25.15       0.00   24944   0 policyHost
      9     1.77       7.64       0.00    1780   0 powercfg
     10    26.67      32.18       0.00    7028   0 powercfg
      8    26.55      31.59       0.00   13600   0 powercfg
      9     1.66       7.55       0.00   22620   0 powercfg
     21     6.17       4.54     202.20   12536   1 PowerMgr
     42    84.26      12.71   2,488.84   20588   1 powershell
     27    47.07      45.38       2.05   25988   1 powershell
     27    24.45       5.31       0.00   12364   0 PresentationFontCache
     92   112.04      13.36      82.30   13176   1 pwsh
    106   163.73      93.21     302.25   14620   1 pwsh
    227   764.01      92.16   1,757.22   25328   1 pwsh

To filter your results for PowerShell, pipe your results to the Where-Object cmdlet.

範例 3:將資訊寫入檔案

在這個例子中,你將函式中的資訊流重新導向到 Info.txt 使用程式碼 6>。 打開 Info.txt 檔案時,你會看到「給你看」的文字。

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

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

在這個範例中,你可以用 Write-Information 來寫出 Get-Process 經過多個管線的物件輸出中,CPU 使用率最高的前 10 個程序。

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}

範例 5:將資訊記錄儲存到變數

利用 InformationVariable 參數,你可以將資訊記錄儲存到變數中。 這讓你可以在腳本後面檢查資訊流訊息。

Get-Process -Id $PID |
    Select-Object ProcessName, CPU, Path |
    Write-Information -Tags 'PowerShell' -InformationVariable 'InfoMsg'
$InfoMsg | Select-Object *
MessageData     : @{ProcessName=pwsh; CPU=12.36; Path=/opt/microsoft/powershell/7/pwsh}
Source          : Write-Information
TimeGenerated   : 10/19/2023 11:28:15
Tags            : {PowerShell}
User            : sdwheeler
Computer        : circumflex
ProcessId       : 237
NativeThreadId  : 261
ManagedThreadId : 10

參數

-MessageData

指定一個資訊訊息,你想在使用者執行腳本或指令時顯示。 為了達到最佳效果,請將資訊訊息以引號包圍。

參數屬性

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

參數集

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

-Tags

一個或多個字串,可以用來排序和過濾你已加入資訊流 Write-Information的訊息。 此參數的運作方式類似於 中的 New-ModuleManifestTags 參數。

參數屬性

類型:

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。

輸入

Object

你可以將物件管道傳送到這個指令集的資訊流。

輸出

None

此 Cmdlet 不會傳回任何輸出。 它只會寫入資訊訊息串流。