共用方式為


Receive-PSSession

針對已中斷連線的工作階段取得其命令結果

Syntax

Receive-PSSession
       [-Session] <PSSession>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       [-Id] <Int32>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       [-ComputerName] <String>
       [-ApplicationName <String>]
       [-ConfigurationName <String>]
       -Name <String>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-Credential <PSCredential>]
       [-Authentication <AuthenticationMechanism>]
       [-CertificateThumbprint <String>]
       [-Port <Int32>]
       [-UseSSL]
       [-SessionOption <PSSessionOption>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       [-ComputerName] <String>
       [-ApplicationName <String>]
       [-ConfigurationName <String>]
       -InstanceId <Guid>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-Credential <PSCredential>]
       [-Authentication <AuthenticationMechanism>]
       [-CertificateThumbprint <String>]
       [-Port <Int32>]
       [-UseSSL]
       [-SessionOption <PSSessionOption>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       [-ConfigurationName <String>]
       [-ConnectionUri] <Uri>
       [-AllowRedirection]
       -InstanceId <Guid>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-Credential <PSCredential>]
       [-Authentication <AuthenticationMechanism>]
       [-CertificateThumbprint <String>]
       [-SessionOption <PSSessionOption>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       [-ConfigurationName <String>]
       [-ConnectionUri] <Uri>
       [-AllowRedirection]
       -Name <String>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-Credential <PSCredential>]
       [-Authentication <AuthenticationMechanism>]
       [-CertificateThumbprint <String>]
       [-SessionOption <PSSessionOption>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       -InstanceId <Guid>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Receive-PSSession
       -Name <String>
       [-OutTarget <OutTarget>]
       [-JobName <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]

Description

Receive-PSSession Cmdlet 會取得在 Windows PowerShell 會話中執行的命令結果, (已中斷連線的 PSSession) 。 若工作階段目前已連線,則 Receive-PSSession 可針對工作階段中斷連線時所執行的命令,而取得其結果。 若工作階段仍處於中斷連線的狀態,則 Receive-PSSession 會連線至工作階段、繼續暫停的命令,並取得工作階段中執行命令的結果。

除了 或 之外,您還可以使用 Receive-PSSession ,而不是使用 Connect-PSSession 命令。 Receive-PSSession 可以連線到任何已中斷連線或重新連線的會話。 這些包括在其他會話或其他計算機上啟動的會話。

Receive-PSSession 適用於刻意中斷的 PSSession ,例如使用 Disconnect-PSSession Cmdlet 或 Invoke-Command Cmdlet 的 InDisconnectedSession 參數,或非預期的,例如網路中斷。

若您使用 Receive-PSSession Cmdlet 連線的工作階段中沒有執行中或暫停的命令,則 Receive-PSSession 會連線至工作階段,但不會傳回輸出或錯誤。

如需中斷連線工作階段功能的詳細資訊,請參閱 about_Remote_Disconnected_Sessions

此 Cmdlet 是在 Windows PowerShell 3.0 中引進。

範例

範例 1:連線到 PSSession

PS C:\> Receive-PSSession -ComputerName Server01 -Name ITTask

此命令使用 Receive-PSSession Cmdlet 連線至 Server01 電腦上的 ITTask 工作階段,並取得工作階段中執行命令的結果。

因為命令不使用 OutTarget 參數,結果將顯示在命令列中。

範例 2:取得已中斷聯機會話上所有命令的結果

PS C:\> Get-PSSession -ComputerName Server01, Server02 | Receive-PSSession

此命令將取得 Server01 與 Server02 電腦上所有已中斷連線工作階段中所有執行命令的結果。

若有任何工作階段未中斷連線或未執行命令,則 Receive-PSSession 不會連線至工作階段且不會傳回任何輸出或錯誤。

範例 3:取得在會話中執行的腳本結果

PS C:\> Receive-PSSession -ComputerName Server01 -Name ITTask -OutTarget Job -JobName ITTaskJob01 -Credential Domain01\Admin01
Id     Name            State         HasMoreData     Location
--     ----            -----         -----------     --------
16     ITTaskJob01     Running       True            Server01

此命令使用 Receive-PSSession Cmdlet,取得 Server01 電腦上 ITTask 工作階段中執行指令碼的結果。

命令使用 ComputerNameName 參數,識別已中斷連線的工作階段。 它使用值為 Job 的 OutTarget 參數指示 Receive-PSSession 傳回結果為工作,並使用 JobName 參數來指定重新連線工作階段中的工作名稱。

此命令會使用 Credential 參數,使用網域管理員的許可權來執行 Receive-PSSession 命令。

輸出顯示 Receive-PSSession 在目前工作階段中傳回結果為工作。 若要取得作業結果,請使用 Receive-Job 命令

範例 4:在網路中斷后取得結果

The first command uses the New-PSSession cmdlet to create a session on the Server01 computer. The command saves the session in the $s variable.The second command gets the session in the $s variable. Notice that the **State** is Opened and the **Availability** is Available. These values indicate that you are connected to the session and can run commands in the session.
PS C:\> $s = New-PSSession -ComputerName Server01 -Name AD -ConfigurationName ADEndpoint
PS C:\> $s

Id Name    ComputerName    State         ConfigurationName     Availability
 -- ----    ------------    -----         -----------------     ------------
  8 AD      Server01        Opened        ADEndpoint            Available

The third command uses the Invoke-Command cmdlet to run a script in the session in the $s variable.The script begins to run and return data, but a network outage occurs that interrupts the session. The user has to exit the session and restart the local computer.
PS> Invoke-Command -Session $s -FilePath \\Server12\Scripts\SharedScripts\New-ADResolve.ps1
 Running "New-ADResolve.ps1"

# Network outage
# Restart local computer
# Network access is not re-established within 4 minutes

When the computer restarts, the user starts Windows PowerShell and runs a Get-PSSession command to get sessions on the Server01 computer. The output shows that the AD session still exists on the Server01 computer. The **State** indicates that it is disconnected and the **Availability** value, None, indicates that it is not connected to any client sessions.
PS C:\> Get-PSSession -ComputerName Server01

 Id Name    ComputerName    State         ConfigurationName     Availability
 -- ----    ------------    -----         -----------------     ------------
  1 Backup  Server01        Disconnected  Microsoft.PowerShell          None
  8 AD      Server01        Disconnected  ADEndpoint                   None


The fifth command uses the **Receive-PSSession** cmdlet to reconnect to the AD session and get the results of the script that ran in the session. The command uses the *OutTarget* parameter to request the results in a job named ADJob.The command returns a job object. The output indicates that the script is still running.
PS C:\> Receive-PSSession -ComputerName Server01 -Name AD -OutTarget Job -JobName AD
Job Id     Name      State         HasMoreData     Location
--     ----      -----         -----------     --------
16     ADJob     Running       True            Server01

The sixth command uses the Get-PSSession cmdlet to check the job state. The output confirms that, in addition to resuming script execution and getting the script results, the **Receive-PSSession** cmdlet reconnected to the AD session, which is now open and available for commands.
PS C:\> Get-PSSession -ComputerName Server01
Id Name    ComputerName    State         ConfigurationName     Availability
-- ----    ------------    -----         -----------------     ------------
 1 Backup  Server01        Disconnected  Microsoft.PowerShell          Busy
 8 AD      Server01        Opened        ADEndpoint                Available

此範例使用 Receive-PSSession Cmdlet,於網路中斷而中斷工作階段連線之後,取得工作的結果。 Windows PowerShell 每隔四分鐘自動嘗試重新聯機會話一次,而且只有在四分鐘間隔中的所有嘗試都失敗時,才會放棄工作。

範例 5:重新連線到已中斷連線的會話

The first command uses the Invoke-Command cmdlet to run a script on the three remote computers. Because the scripts gathers and summarize data from multiple databases, it often takes the script an extended time to finish. The command uses the *InDisconnectedSession* parameter, which starts the scripts and then immediately disconnects the sessions.The command uses the *SessionOption* parameter to extend the **IdleTimeout** value of the disconnected session. Disconnected sessions are considered to be idle from the moment they are disconnected, so it is important to set the idle time-out for long enough that the commands can complete and you can reconnect to the session, if necessary. You can set the **IdleTimeout** only when you create the **PSSession** and change it only when you disconnect from it. You cannot change the **IdleTimeout** value when you connect to a **PSSession** or receiving its results.After running the command, the user exits Windows PowerShell and closes the computer .
PS C:\> Invoke-Command -InDisconnectedSession -ComputerName Server01, Server02, Server30 -FilePath \\Server12\Scripts\SharedScripts\Get-BugStatus.ps1 -Name BugStatus -SessionOption @{IdleTimeout = 86400000} -ConfigurationName ITTasks# Exit

# Start Windows PowerShell on a different computer.

On the next day, the user resumes Windows and starts Windows PowerShell. The second command uses the Get-PSSession cmdlet to get the sessions in which the scripts were running. The command identifies the sessions by the computer name, session name, and the name of the session configuration and saves the sessions in the $s variable.The third command displays the value of the $s variable. The output shows that the sessions are disconnected, but not busy, as expected.
PS C:\> $s = Get-PSSession -ComputerName Server01, Server02, Server30 -Name BugStatus
 PS C:\> $s
Id Name    ComputerName    State         ConfigurationName     Availability
 -- ----    ------------    -----         -----------------     ------------
  1 ITTask  Server01        Disconnected  ITTasks                       None
  8 ITTask  Server02        Disconnected  ITTasks                       None
  2 ITTask  Server30        Disconnected  ITTasks                       None


The fourth command uses the **Receive-PSSession** cmdlet to connect to the sessions in the $s variable and get their results. The command saves the results in the $Results variable.Another display of the $s variable shows that the sessions are connected and available for commands.
PS C:\> $Results = Receive-PSSession -Session $s
PS C:\> $s
 Id Name    ComputerName    State         ConfigurationName     Availability
-- ----    ------------    -----         -----------------     ------------
 1 ITTask  Server01        Opened        ITTasks                  Available
 8 ITTask  Server02        Opened        ITTasks                  Available
 2 ITTask  Server30        Opened        ITTasks                  Available


The fifth command displays the script results in the $Results variable. If any of the results are unexpected, the user can run commands in the sessions to investigate.
PS C:\> $Results
Bug Report - Domain 01
----------------------
ComputerName          BugCount          LastUpdated
--------------        ---------         ------------
Server01              121               Friday, December 30, 2011 5:03:34 PM

此範例使用 Receive-PSSession Cmdlet 重新連線至刻意中斷連線的工作階段,並取得工作階段中執行工作的結果。

範例 6:在中斷連線的會話中執行作業

The first command uses the New-PSSession cmdlet to create the Test session on the Server01 computer. The command saves the session in the $s variable.
PS C:\> $s = New-PSSession -ComputerName Server01 -Name Test

The second command uses the Invoke-Command cmdlet to run a command in the session in the $s variable. The command uses the *AsJob* parameter to run the command as a job and to create the job object in the current session. The command returns a job object, which is saved in the $j variable.The third command displays the job object in the $j variable.
PS C:\> $j = Invoke-Command -Session $s { 1..1500 | Foreach-Object {"Return $_"; sleep 30}} -AsJob

PS C:\> $j
Id     Name           State         HasMoreData     Location
--     ----           -----         -----------     --------
16     Job1           Running       True            Server01

The fourth command disconnects the session in the $s variable.
PS C:\> $s | Disconnect-PSSession
Id Name   ComputerName    State         ConfigurationName     Availability
-- ----   ------------    -----         -----------------     ------------
1  Test   Server01        Disconnected  Microsoft.PowerShell  None

The fifth command shows the effect of disconnecting on the job object in the $j variable. The job state is now Disconnected.
PS C:\> $j
Id     Name           State         HasMoreData     Location
--     ----           -----         -----------     --------
16     Job1           Disconnected  True            Server01

The sixth command runs a Receive-Job command on the job in the $j variable. The output shows that the job began to return output before the session and the job were disconnected.
PS C:\> Receive-Job $j -Keep
Return 1
Return 2

The seventh command is run in the same client session. The command uses the Connect-PSSession cmdlet to reconnect to the Test session on the Server01 computer and saves the session in the $s2 variable.
PS C:\> $s2 = Connect-PSSession -ComputerName Server01 -Name Test

The eighth command uses the **Receive-PSSession** cmdlet to get the results of the job that was running in the session. Because the command is run in the same session, **Receive-PSSession** returns the results as a job by default and reuses the same job object. The command saves the job in the $j2 variable.The ninth command uses the **Receive-Job** cmdlet to get the results of the job in the $j variable.
PS C:\> $j2 = Receive-PSSession -ComputerName Server01 -Name Test

PS C:\> Receive-Job $j
Return 3
Return 4

此範例顯示在已中斷連線工作階段中執行的工作會有什麼變化。

參數

-AllowRedirection

指出此 Cmdlet 允許將此連線重新導向至替代的統一資源標識碼 (URI) 。

使用 ConnectionURI 參數時,遠端目的地可傳回重新導向至不同 URI 的指示。 根據預設,Windows PowerShell 不會重新導向連線,但您可以使用此參數來重新導向連線。

您也可以變更 MaximumConnectionRedirectionCount 工作階段選項值,限制連線重新導向的次數。 使用 New-PSSessionOption Cmdlet 的 MaximumRedirection 參數,或設定$PSSessionOption喜好設定變數的 MaximumConnectionRedirectionCount 屬性。 預設值為 5。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ApplicationName

指定應用程式。 此 Cmdlet 只會連線到使用指定應用程式的作業階段。

輸入連線 URI 的應用程式名稱區段。 例如,在下列連線 URI 中,應用程式名稱為 WSMan: https://localhost:5985/WSMAN。 工作階段的應用程式名稱儲存在工作階段的 Runspace.ConnectionInfo.AppName 屬性中。

此參數的值可用來選取與篩選工作階段。 它不會變更工作階段使用的應用程式。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Authentication

指定用來驗證命令中使用者認證的機制,以重新連線到已中斷連線的會話。 此參數可接受的值為:

  • Default
  • 基本資訊
  • Credssp
  • Digest
  • Kerberos
  • 交涉
  • NegotiateWithImplicitCredential

預設值為 Default。

如需此參數值的詳細資訊,請參閱 AuthenticationMechanism 列舉

警告

認證安全性支援提供者 (CredSSP) 驗證,其中使用者認證會傳遞至要驗證的遠端電腦,是針對需要驗證多個資源的命令所設計,例如存取遠端網路共用。 此機制會使得遠端作業的安全性風險變高。 若遠端電腦遭到入侵,傳遞給它的認證便可用來控制網路工作階段。

Type:AuthenticationMechanism
Accepted values:Default, Basic, Negotiate, NegotiateWithImplicitCredential, Credssp, Digest, Kerberos
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-CertificateThumbprint

指定具有連線至已中斷連線工作階段權限之使用者帳戶的數位公開金鑰憑證 (X509)。 請輸入憑證的憑證指紋。

憑證將用於用戶端憑證式驗證。 它們只能對應至本機用戶帳戶。 它們不適用於網域帳戶。

若要取得憑證指紋,請在 Windows PowerShell Cert: 磁碟機中使用 Get-Item 或 Get-ChildItem 命令。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ComputerName

指定儲存已中斷連線工作階段的電腦。 會話會儲存在伺服器端或接收連線結尾的電腦上。 預設是本機電腦。

輸入電腦的 NetBIOS 名稱、IP 位址或完整網域名稱。 不允許使用萬用字元。 若要指定本機計算機,請輸入計算機名稱、localhost 或 dot (.)

Type:String
Aliases:Cn
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-ConfigurationName

只連線使用指定工作階段設定的工作階段。

輸入工作階段設定的設定名稱或完整資源 URI。 如果您只指定組態名稱,則會在前面加上下列架構 URI: https://schemas.microsoft.com/powershell。 工作階段的設定名稱儲存在工作階段的 ConfigurationName 屬性中。

此參數的值可用來選取與篩選工作階段。 它不會變更工作階段使用的工作階段設定。

如需會話設定的詳細資訊,請參閱 about_Session_Configurations

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Confirm

在執行 Cmdlet 前提示您確認。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ConnectionUri

指定 URI,定義用來重新連線到已中斷聯機會話的連接端點。

此 URI 必須是完整的 URI。 這個字串的格式如下所示:

<Transport>://<ComputerName>:<Port>/<ApplicationName>

預設值如下:

https://localhost:5985/WSMAN

若未指定連線 URI,您可以使用 UseSSLComputerNamePortApplicationName 參數指定連線 URI 值。

URI 的 Transport 區段有效值為 HTTP 與 HTTPS。 若指定含傳輸區段的連線 URI,但未指定連接埠,將會以標準連接埠 80 (適用於 HTTP) 和 443 (適用於 HTTPS) 建立工作階段。 若要使用預設連接埠於 Windows PowerShell 遠端功能,請為 HTTP 指定連接埠 5985,或是為 HTTPS 指定連接埠 5986。

若目的地電腦將連線重新導向至不同 URI,除非您在命令中使用 AllowRedirection 參數,否則 Windows PowerShell 會禁止重新導向。

Type:Uri
Aliases:URI, CU
Position:0
Default value:https://localhost:5985/WSMAN
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Credential

指定具有連線至已中斷連線工作階段權限的使用者帳戶。 預設為目前使用者。

輸入使用者名稱,例如 User01 或 Domain01\User01。 或者,輸入 PSCredential 物件,例如 Get-Credential Cmdlet 所產生的物件。 如果您輸入使用者名稱,此 Cmdlet 會提示您輸入密碼。

Type:PSCredential
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Id

指定已中斷連線工作階段的識別碼。 Id 參數只有在先前已中斷連線的會話已連線到目前的會話時才能運作。

當工作階段儲存在本機電腦,但是未連線目前工作階段,則此參數有效,但沒有作用。

Type:Int32
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-InstanceId

指定已中斷連線工作階段的執行個體識別碼。

實例標識碼是 GUID,可唯一識別本機或遠端電腦上的 PSSession

實例標識碼會儲存在 PSSessionInstanceID 屬性中。

Type:Guid
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-JobName

Receive-PSSession 傳回的工作,指定好記的名稱。

Receive-PSSession 會在 OutTarget 參數的值為 Job 或正在中斷聯機會話中執行的作業在目前工作階段中啟動時,傳回作業。

若已中斷連線工作階段中執行的工作是在目前工作階段中啟動,Windows PowerShell 會重複使用工作階段中的原始工作物件,並忽略 JobName 參數的值。

若已中斷連線工作階段中執行的工作是在不同工作階段中啟動,Windows PowerShell 會建立新的工作物件。 它會使用預設名稱,但是您可以使用此參數來變更名稱。

如果 OutTarget 參數的預設值或明確值不是 Job,命令會成功,但 JobName 參數沒有任何作用。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Name

指定已中斷連線工作階段的好記名稱。

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-OutTarget

決定如何傳回工作階段結果。 此參數可接受的值為:

  • 作業。 以非同步方式,傳回工作物件中的結果。 您可以使用 JobName 參數,指定工作的名稱或新名稱。
  • 主控件。 將結果傳回命令列 (同步)。 若正在繼續命令,或結果包含大量物件,則回應可能會延遲。

OutTarget 參數的預設值為 Host。 不過,如果已中斷連線工作階段中正在接收的命令是在目前工作階段中啟動,OutTarget 參數的預設值則為命令啟動的格式。 若命令啟動為工作,則預設會以工作傳回。 否則,預設會傳回主機程式。

主機程式在命令列顯示傳回的物件時,通常不會有延遲,但是也可能不同。

Type:OutTarget
Accepted values:Default, Host, Job
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Port

指定遠端電腦上用來重新連線至工作階段的網路連接埠。 若要連線到遠端電腦,遠端電腦必須接聽連線使用的連接埠。 默認埠為 5985,這是 HTTP 的 WinRM 連接埠,而 5986 是 HTTPS 的 WinRM 連接埠。

使用替代連接埠之前,必須先將遠端電腦上的 WinRM 接聽程式設定為在該連接埠進行接聽。 若要設定接聽程式,請在 Windows PowerShell 提示字元中輸入下列兩個命令:

Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse

New-Item -Path WSMan:\Localhost\listener -Transport http -Address * -Port \<port-number\>

除非必要,否則請勿使用 Port 參數。 在命令中設定的連接埠,將套用於執行命令所在的所有電腦或工作階段。 替代的連接埠設定可能使得命令無法在全部電腦上執行。

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Session

指定已中斷連線的工作階段。 輸入包含 PSSession 的變數,或建立或取得 PSSession 的命令,例如 Get-PSSession 命令。

Type:PSSession
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-SessionOption

指定工作階段的進階選項。 輸入 SessionOption 物件 (例如您使用 New-PSSessionOption Cmdlet 建立的物件) 或者雜湊表,其中索引鍵為工作階段選項名稱,而值為工作階段選項值。

選項的預設值是由 $PSSessionOption 喜好設定變數的值所決定 (若設定了該變數)。 否則,將以工作階段設定中設定的選項建立預設值。

工作階段選項值優先順序高於 $PSSessionOption 喜好設定變數與工作階段設定中設定的工作階段預設值。 不過,它們的優先順序不會高於工作階段設定中設定的最大值、配額或限制。

如需包含預設值的會話選項描述,請參閱New-PSSessionOption。 如需 $PSSessionOption 喜好設定變數的相關信息,請參閱 about_Preference_Variables。 如需會話設定的詳細資訊,請參閱 about_Session_Configurations

Type:PSSessionOption
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-UseSSL

表示此 Cmdlet 使用安全套接字層 (SSL) 通訊協議來連線到已中斷連線的會話。 預設不會使用 SSL。

WS-Management 會加密透過網路傳輸的所有 Windows PowerShell 內容。 UseSSL 為額外的保護,可使用 HTTPS 連線取代 HTTP 連線來傳送資料。

如果您使用此參數,但在用於命令的埠上無法使用 SSL,命令就會失敗。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

顯示執行 Cmdlet 後會發生的情況。 Cmdlet 並不會執行。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

PSSession

您可以使用管線將會話物件,例如由 Get-PSSession Cmdlet 傳回的工作階段物件傳送至此 Cmdlet。

Int32

您可以使用管線將會話標識碼傳送至此 Cmdlet。

Guid

您可以使用管線傳送此 Cmdlet 的作業階段實例識別碼。

String

您可以使用管線將會話名稱傳送至此 Cmdlet。

輸出

System.Management.Automation.Job or PSObject

如果有任何命令,此 Cmdlet 會傳回在中斷聯機會話中執行的命令結果。 如果 OutTarget 參數的值或預設值是 Job,Receive-PSSession 會傳回作業物件。 否則,它會傳回代表該命令結果的物件。

備註

  • Receive-PSSession 只會從已中斷連線的會話取得結果。 只有連線到或終止於執行 Windows PowerShell 3.0 或更新版本的電腦的會話可以中斷連線並重新連線。

  • 若在已中斷連線工作階段中執行的命令未產生結果,或是結果已經傳回其他工作階段,Receive-PSSession 將不會產生任何輸出。

  • 工作階段的輸出緩衝處理模式決定工作階段中斷連線時,工作階段中的命令如何管理輸出。 當會話的 OutputBufferingMode 選項值為 Drop 且輸出緩衝區已滿時,命令會開始刪除輸出。 Receive-PSSession 無法復原此輸出。 如需輸出緩衝模式選項的詳細資訊,請參閱 New-PSSessionOption 和 New-PSTransportOption Cmdlet 的說明主題。

  • 當您連線到 PSSession 或接收結果時,您無法變更 PSSession 的閒置逾時值。 Receive-PSSessionSessionOption 參數會採用具有IdleTimeout值的 SessionOption 物件。 不過,當$PSSessionOption變數連接到 PSSession 或接收結果時,會忽略 SessionOption 物件的 IdleTimeout 值和 $PSSessionOption 變數的 IdleTimeout 值。

    當您建立 PSSession 時,您可以使用 New-PSSession 或 Invoke-Command Cmdlet,以及當您中斷與 PSSession 的連線時,設定並變更 PSSession 的閒置逾時。

    PSSessionIdleTimeout屬性對於中斷連線的會話很重要,因為它會決定遠端電腦上維護中斷連線的會話多久。 已中斷連線的工作階段一被中斷連線,便被視為閒置,即使命令仍然在已中斷連線的工作階段中執行也是如此。

  • 如果您使用 Invoke-Command Cmdlet 的 AsJob 參數,在遠端會話中啟動作業,即使作業在遠端會話中執行,作業物件還是會在目前的會話中建立。 若中斷連線遠端工作階段,則目前工作階段中的工作物件便會從工作中斷連線。 工作物件仍然包含傳回的結果,但是不會接收已中斷連線工作階段中工作的新結果。

    若不同的用戶端連線包含執行中工作的工作階段,則新連線的工作階段將無法使用傳遞至原始工作階段中原始工作物件的結果。 只有未傳遞至原始工作物件的結果,才能在重新連線的工作階段中使用。

    同樣地,若在工作階段中啟動指令碼,然後從工作階段中斷連線,則連線工作階段的其他用戶端將無法使用中斷連線前指令碼傳遞給工作階段的結果。

    若要避免要中斷連線的工作階段中遺失資料,請使用 Invoke-Command Cmdlet 的 InDisconnectedSession 參數。 因為此參數會避免將結果傳回目前工作階段,因此重新連線至工作階段時,所有結果都可供使用。

    您也可以使用 Invoke-Command Cmdlet 在遠端會話中執行 Start-Job 命令,以防止數據遺失。 在此狀況下,工作物件將建立於遠端工作階段中。 您無法使用 Receive-PSSession Cmdlet 來取得工作結果。 請改用 Connect-PSSession Cmdlet 連線到會話,然後使用 Invoke-Command Cmdlet 在工作階段中執行 Receive-Job 命令。

  • 包含執行工作的工作階段中斷連線後又重新連線時,只有當工作被中斷連線並重新連線至相同工作階段,且重新連線的命令未指定新工作名稱時,才會重複使用原始工作物件。 若工作階段重新連線至不同的用戶端工作階段,或指定了新工作名稱,則 Windows PowerShell 會為新工作階段建立新的工作物件。

  • 當您中斷 PSSession 的連線時,會話狀態為 [已中斷連線],可用性為 [無]。

    State 屬性的值是相對於目前工作階段。 因此,[已中斷連線] 的值表示 PSSession 未連線到目前的會話。 不過,這並不表示 PSSession 已中斷與所有會話的連線。 它可能連線不同的工作階段。 若要判斷是否可以連線或重新連線工作階段,請使用 Availability 屬性。

    Availability 值為 None 表示您可以連線工作階段。 [忙碌] 的值表示您無法連線到 PSSession ,因為它已連線到另一個會話。

    如需會話之 State 屬性值的詳細資訊,請參閱 MSDN 連結庫中的 RunspaceState 列舉

    如需會話 可用性 屬性值的詳細資訊,請參閱 RunspaceAvailability 列舉