Receive-PSSession

获取断开连接的会话中的命令的结果

语法

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>]

说明

Receive-PSSession cmdlet 获取Windows PowerShell会话中运行的命令的结果 (已断开连接的 PSSession) 。 如果会话当前处于连接状态,则 Receive-PSSession 将获取之前该会话断开连接时所运行的命令的结果。 如果会话仍处于断开连接的状态,则 Receive-PSSession 将连接该会话、恢复所有已挂起的命令并获取在该会话中运行的命令的结果。

除了或而不是 Connect-PSSession 命令之外,还可以使用 Receive-PSSessionReceive-PSSession 可以连接到任何断开连接或重新连接的会话。 其中包括在其他会话或其他计算机上启动的会话。

Receive-PSSession 适用于有意断开连接的 PSSession ,例如使用 Invoke-Command cmdlet 的 Disconnect-PSSession 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
  • 摘要
  • Kerberos
  • Negotiate
  • 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 或点 (.)

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。 如果使用 Transport 段指定连接 URI,但不指定端口,将使用以下标准端口来创建会话:80 用于 HTTP,443 用于 HTTPS。 若要使用 Windows PowerShell 远程处理的默认端口,请指定 HTTP 端口 5985 或 HTTPS 端口 5986。

如果目标计算机将连接重定向到另一个 URI,Windows PowerShell 将阻止重定向,除非你在命令中使用了 AllowRedirection 参数。

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。 仅当断开连接的会话以前连接到当前会话时, Id 参数才有效。

如果会话存储在本地计算机上,但未连接到当前会话,则此参数是有效的,但并未生效。

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

-InstanceId

指定断开连接的会话的实例 ID。

实例 ID 是一个 GUID,用于唯一标识本地或远程计算机上的 PSSession

实例 ID 存储在 PSSessionInstanceID 属性中。

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

-JobName

指定 Receive-PSSession 返回的作业的友好名称。

OutTarget 参数的值为 Job 或在断开连接的会话中运行的作业在当前会话中启动时,Receive-PSSession 将返回作业。

如果断开连接的会话中正在运行的作业已在当前会话中启动,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

可以通过管道将会话 ID 传递给此 cmdlet。

Guid

可以通过管道传递此 cmdlet 会话的实例 ID。

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 参数采用值为 IdleTimeoutSessionOption 对象。 但是,在连接到 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 属性的值是相对于当前会话的。 因此,值 Disconnected 表示 PSSession 未连接到当前会话。 但是,这并不意味着 PSSession 与所有会话断开连接。 它可能连接到另一个会话。 若要确定是否可以连接或重新连接到该会话,请使用 Availability 属性。

    Availability 的 None 值指示可连接会话。 如果值为 Busy,则表示无法连接到 PSSession ,因为它已连接到另一个会话。

    有关会话的 State 属性的值的详细信息,请参阅 MSDN 库中的 RunspaceState 枚举

    有关会话的 Availability 属性的值的详细信息,请参阅 RunspaceAvailability 枚举