about_Remote_Output

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Remote_Output

簡短描述

描述如何解譯和格式化遠端命令的輸出。

詳細描述

在遠端電腦執行命令的輸出,看起來像在本機電腦上執行相同命令的輸出,但有一些重大的差異。

本主題說明如何解譯、格式化及顯示在遠端電腦上執行的命令輸出。

顯示電腦名稱

當您使用 Invoke-Command Cmdlet 在遠端電腦上執行命令時,此命令會傳回包含產生資料之電腦名稱的物件。遠端電腦名稱會儲存在 PSComputerName 屬性。

對大部分命令而言,PSComputerName 依預設會顯示。例如,下列命令會在兩部遠端電腦 Server01 和 Server02 上執行 Get-Culture 命令。輸出會出現在下方,其中包含命令執行所在的遠端電腦名稱。

        C:\PS> invoke-command -script {get-culture} -comp Server01, Server02

        LCID  Name    DisplayName                PSComputerName
        ----  ----    -----------                --------------
        1033  en-US   English (United States)    Server01
        1033  es-AR   Spanish (Argentina)        Server02

您可以使用 Invoke-Command 的 HideComputerName 參數來隱藏 PSComputerName 屬性。這個參數係針對只從一部遠端電腦上收集資料的命令所設計。

下列命令會在 Server01 遠端電腦上執行 Get-Culture 命令。它會使用 HideComputerName 參數來隱藏 PSComputerName 屬性和相關的屬性。

        C:\PS> invoke-command -scr {get-culture} -comp Server01 -HideComputerName

        LCID             Name             DisplayName
        ----             ----             -----------
        1033             en-US            English (United States)

如果預設不顯示,則您也可以顯示 PSComputerName 屬性。

例如,下列命令會使用 Format-Table Cmdlet,將 PSComputerName 屬性加入遠端 Get-Date 命令的輸出。

        C:\PS> $dates = invoke-command -script {get-date} -computername Server01, Server02
        C:\PS> $dates | format-table DateTime, PSComputerName -auto

        DateTime                            PSComputerName
        --------                            --------------
        Monday, July 21, 2008 7:16:58 PM    Server01
        Monday, July 21, 2008 7:16:58 PM    Server02      

顯示 MACHINENAME 屬性

包括 Get-Process、Get-Service 和 Get-EventLog 在內的數個 Cmdlet,都有能在遠端電腦上取得物件的 ComputerName 參數。這些 Cmdlet 不使用 Windows PowerShell® 遠端,所以即使在未針對 Windows PowerShell 遠端設定的電腦上也可以使用。

這些 Cmdlet 傳回的物件會儲存在 MachineName 屬性的遠端電腦名稱。(這些物件沒有 PSComputerName 屬性。)

例如,這個命令會取得 Server01 和 Server02 遠端電腦上的 PowerShell 處理程序。預設顯示不包含 MachineName 屬性。

        C:\PS> get-process PowerShell -computername server01, server02


        Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
        -------  ------    -----      ----- -----   ------     -- -----------
            920      38    97524     114504   575     9.66   2648 PowerShell
            194       6    24256      32384   142            3020 PowerShell
            352      27    63472      63520   577     3.84   4796 PowerShell

Format-Table Cmdlet 可用來顯示處理程序物件的 MachineName 屬性。

例如,下列命令會將處理程序儲存在 $p 變數,然後使用管線運算子 (|) 將 $p 中的處理程序傳送至 Format-Table 命令。該命令會使用 Format-Table 的 Property 參數,藉此在顯示中包含 MachineName 屬性。

        C:\PS> $p = get-process PowerShell -comp Server01, Server02
        C:\PS> $P | format-table -property ID, ProcessName, MachineName -auto


        Id ProcessName MachineName
        -- ----------- -----------
        2648 PowerShell  Server02
        3020 PowerShell  Server01
        4796 PowerShell  Server02

下列更複雜的命令會將 MachineName 屬性加入預設處理程序顯示。它會使用雜湊表來指定計算的屬性。幸運的是,您不必了解這一點也能使用它。

(請注意倒單引號 [`] 是接續字元。)

        C:\PS> $p = get-process PowerShell -comp Server01, Server02

        C:\PS> $p | format-table -property Handles, `
                    @{Label="NPM(K)";Expression={[int]($_.NPM/1024)}}, `
                    @{Label="PM(K)";Expression={[int]($_.PM/1024)}}, `
                    @{Label="WS(K)";Expression={[int]($_.WS/1024)}}, `
                    @{Label="VM(M)";Expression={[int]($_.VM/1MB)}}, `
                    @{Label="CPU(s)";Expression={if ($_.CPU -ne $()){ $_.CPU.ToString("N")}}}, `
                    Id, ProcessName, MachineName -auto
       

        Handles NPM(K) PM(K)  WS(K) VM(M) CPU(s)   Id ProcessName MachineName
        ------- ------ -----  ----- ----- ------   -- ----------- -----------
            920     38 97560 114532   576        2648 PowerShell  Server02     
            192      6 24132  32028   140        3020 PowerShell  Server01   
            438     26 48436  59132   565        4796 PowerShell  Server02   

已還原序列化的物件

當您執行產生輸出的遠端命令時,該命令輸出會透過網路傳送回本機電腦。

因為大部分即時 Microsoft.NET Framework 物件 (例如 Windows PowerShell Cmdlet 傳回的物件) 無法透過網路傳送,所以會將作用中的物件「序列化」。換句話說,作用中的物件會轉換成物件和其屬性的 XML 表示法。然後 XML 型序列化物件會透過網路傳輸。

在本機電腦上,Windows PowerShell 會接收 XML 型的序列化物件,然後將其還原序列化,方法是將 XML 型物件轉換為標準的 .NET Framework 物件。

不過,還原序列化的物件不是即時的物件。其為物件在序列化時的快照,且包含屬性,但不包含方法。您可以在 Windows PowerShell 使用和管理這些物件,包括傳入管線、顯示選取的屬性和格式化。

大多數已還原序列化的物件會自動格式化為依據 Types.ps1xml 或 Format.ps1xml 檔案中的項目顯示。不過,對於在遠端電腦上所產生的所有已還原序列化物件,本機電腦可能沒有這些物件的格式檔案。當物件未格式化時,每個物件的所有屬性都會以資料流清單顯示在主控台。

當物件未自動格式化時,您可以使用格式化 Cmdlet,例如 Format-Table 或 Format-List,格式化選取的屬性並加以顯示。或者,您可以使用 Out-GridView Cmdlet 來顯示資料表中的物件。

此外,如果您在遠端電腦上使用本機電腦上所沒有的 Cmdlet 來執行命令,則此命令傳回的物件可能無法正確格式化,因為您的電腦上沒有這些物件的格式化檔案。若要從另一部電腦取得格式化資料,請使用 Get-FormatData 和 Export-FormatData Cmdlet。

當您收到某些物件類型 (例如 DirectoryInfo 物件和 GUID) 時,會將這些類型轉換回即時物件。這些物件不需要任何特殊處理或格式化。

排序結果

Cmdlet 之 ComputerName 參數中的電腦名稱順序,會決定 Windows PowerShell 連線到遠端電腦的順序。不過,此結果會以本機電腦接收的順序出現,這可能是不同的順序。

若要變更該結果的順序,請使用 Sort-Object Cmdlet。您可以根據 PSComputerName 或 MachineName 屬性排序。您也可以根據另一個該物件的屬性排序,如此一來不同電腦的結果就會顛倒。

另請參閱

about_Remote

about_Remote_Variables

Format-Table

Get-EventLog

Get-Process

Get-Service

Get-WmiObject

Invoke-Command

Out-GridView

Select-Object