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

範例

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

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

範例 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:傳遞物件以寫入資訊

在此範例中,您可以使用 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}

範例 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

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

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

您可以使用管線將 對象傳遞至資訊資料流至此 Cmdlet。

輸出

None

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