共用方式為


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、作業和排程的工作。

注意

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

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

注意

從 Windows PowerShell 5.0 開始,Write-HostWrite-Information 的包裝器。這允許您使用 Write-Host 將輸出發送到信息流。 這可讓您擷取或隱藏使用 Write-Host 寫入的數據,同時保留回溯相容性。 如需詳細資訊,請參閱 Write-Host

Write-Information 也是 Windows PowerShell 5.1 中支援的工作流程活動。

範例

範例 1:撰寫 Get- results 的資訊

在此範例中,您會在執行 Get-Process 命令之前,先顯示資訊訊息「以 『P』 開頭的進程」,以尋找所有具有開頭為 『p』 Name 值的進程。 因為 $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

範例 2:寫入資訊並將其標記

在此範例中,您會使用 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:將資訊記錄儲存至變數

您可以使用 InformationVariable 參數,將資訊記錄儲存至變數。 這可讓您稍後在腳本中檢查資訊串流訊息。

$psproc = Get-Process -Id $PID | Select-Object ProcessName, CPU, Path
Write-Information -MessageData $psproc -Tags 'PowerShell' -InformationVariable 'InfoMsg'
$InfoMsg | Select-Object *
MessageData     : @{ProcessName=powershell; CPU=1.609375;
                  Path=C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe}
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
別名:味精

參數集

(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

您無法使用管線將物件傳送至此 cmdlet。

輸出

None

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