Connect-PSSession

重新连接到已断开连接的会话。

语法

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

说明

Connect-PSSession cmdlet (断开连接的 PSSession) 重新连接到用户管理的Windows PowerShell会话。 它可以在有意断开连接的会话(例如通过使用 Disconnect-PSSession cmdlet 或 Invoke-Command cmdlet 的 InDisconnectedSession 参数)和无意断开连接的会话(例如由于临时网络故障)上运行。

Connect-PSSession 可以连接到由同一用户启动的任何断开连接的会话。 其中包括由其他计算机上的其他会话启动或断开连接的会话。

但是,Connect-PSSession 无法连接到已损坏的或已关闭的会话或者通过使用 Enter-PSSession cmdlet 启动的交互式会话。 此外,也无法将会话连接到由其他用户启动的会话,除非你能提供创建会话的用户的凭据。

有关断开连接会话的功能的详细信息,请参阅 about_Remote_Disconnected_Sessions。

此 cmdlet 是在 Windows PowerShell 3.0 中引入的。

示例

示例 1:重新连接到会话

PS C:\> Connect-PSSession -ComputerName Server01 -Name ITTask
Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 4 ITTask          Server01        Opened        ITTasks                  Available

此命令重新连接到 Server01 计算机上的 ITTask 会话。

该输出显示此命令已成功。 会话 的状态 为“已打开”, “可用性” 为“可用”,表示可以在会话中运行命令。

示例 2:断开连接和重新连接的影响

PS C:\> Get-PSSession

Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 Backups         Localhost       Opened        Microsoft.PowerShell     Available


PS C:\> Get-PSSession | Disconnect-PSSession

Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 Backups         Localhost       Disconnected  Microsoft.PowerShell          None


PS C:\> Get-PSSession | Connect-PSSession

Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 Backups         Localhost       Opened        Microsoft.PowerShell     Available

此示例显示了断开某一会话的连接又重新连接到该会话的效果。

第一个命令使用 Get-PSSession cmdlet。 在没有 ComputerName 参数的情况下,该命令仅获取已在当前会话中创建的会话。

该输出显示此命令可获取本地计算机上的 Backup 会话。 会话 的状态 为“已打开”, 可用性 为“可用”。

第二个命令使用 Get-PSSession cmdlet 获取在当前会话中创建的 PSSession 对象,并使用 Disconnect-PSSession cmdlet 来断开会话的连接。 该输出显示 Backup 会话已断开连接。 会话 的状态 为“断开连接”, “可用性” 为“无”。

第三个命令使用 Get-PSSession cmdlet 获取在当前会话中创建的 PSSession 对象,并使用 Connect-PSSession cmdlet 重新连接会话。 该输出显示 Backup 会话已重新连接。 会话 的状态 为“已打开”, 可用性 为“可用”。

如果在未断开连接的会话上使用 Connect-PSSession cmdlet,则命令不会影响会话,也不会生成任何错误。

示例 3:企业方案中的一系列命令

The administrator starts by creating a sessions on a remote computer and running a script in the session.The first command uses the **New-PSSession** cmdlet to create the ITTask session on the Server01 remote computer. The command uses the *ConfigurationName* parameter to specify the ITTasks session configuration. The command saves the sessions in the $s variable.
PS C:\> $s = New-PSSession -ComputerName Server01 -Name ITTask -ConfigurationName ITTasks

 The second command **Invoke-Command** cmdlet to start a background job in the session in the $s variable. It uses the *FilePath* parameter to run the script in the background job.
PS C:\> Invoke-Command -Session $s {Start-Job -FilePath \\Server30\Scripts\Backup-SQLDatabase.ps1}
Id     Name            State         HasMoreData     Location             Command
--     ----            -----         -----------     --------             -------
2      Job2            Running       True            Server01             \\Server30\Scripts\Backup...

The third command uses the Disconnect-PSSession cmdlet to disconnect from the session in the $s variable. The command uses the *OutputBufferingMode* parameter with a value of Drop to prevent the script from being blocked by having to deliver output to the session. It uses the *IdleTimeoutSec* parameter to extend the session time-out to 15 hours.When the command is completed, the administrator locks her computer and goes home for the evening.
PS C:\> Disconnect-PSSession -Session $s -OutputBufferingMode Drop -IdleTimeoutSec 60*60*15
Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 ITTask          Server01        Disconnected  ITTasks               None

Later that evening, the administrator starts her home computer, logs on to the corporate network, and starts Windows PowerShell. The fourth command uses the Get-PSSession cmdlet to get the sessions on the Server01 computer. The command finds the ITTask session.The fifth command uses the **Connect-PSSession** cmdlet to connect to the ITTask session. The command saves the session in the $s variable.
PS C:\> Get-PSSession -ComputerName Server01 -Name ITTask

Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 ITTask          Server01        Disconnected  ITTasks               None


PS C:\> $s = Connect-PSSession -ComputerName Server01 -Name ITTask


Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 ITTask          Server01        Opened        ITTasks               Available

The sixth command uses the **Invoke-Command** cmdlet to run a Get-Job command in the session in the $s variable. The output shows that the job finished successfully.The seventh command uses the **Invoke-Command** cmdlet to run a Receive-Job command in the session in the $s variable in the session. The command saves the results in the $BackupSpecs variable.The eighth command uses the **Invoke-Command** cmdlet to runs another script in the session. The command uses the value of the $BackupSpecs variable in the session as input to the script.


PS C:\> Invoke-Command -Session $s {Get-Job}

Id     Name            State         HasMoreData     Location             Command
--     ----            -----         -----------     --------             -------
2      Job2            Completed     True            Server01             \\Server30\Scripts\Backup...

PS C:\> Invoke-Command -Session $s {$BackupSpecs = Receive-Job -JobName Job2}

PS C:\> Invoke-Command -Session $s {\\Server30\Scripts\New-SQLDatabase.ps1 -InitData $BackupSpecs.Initialization}

The ninth command disconnects from the session in the $s variable.The administrator closes Windows PowerShell and closes the computer. She can reconnect to the session on the next day and check the script status from her work computer.
PS C:\> Disconnect-PSSession -Session $s -OutputBufferingMode Drop -IdleTimeoutSec 60*60*15
Id Name            ComputerName    State         ConfigurationName     Availability
-- ----            ------------    -----         -----------------     ------------
 1 ITTask          Server01        Disconnected  ITTasks               None

这一系列的命令演示 Connect-PSSession cmdlet 在企业方案中的可能的使用方式。 在此示例中,系统管理员将在远程计算机上的会话中启动长时间运行的作业。 启动该作业后,管理员将断开与该会话的连接,然后下班回家。 当晚晚些时候,管理员登录到她的家庭计算机,并验证作业是否运行,直到它完成。

参数

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

有关此参数的值的详细信息,请参阅 MSDN 库中的 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:Named
Default value:None
Required:True
Accept pipeline input:False
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 必须完全限定。 此字符串的格式如下:

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

默认值如下:

https://localhost:5985/WSMAN

如果未指定连接 URI,则可以使用 UseSSLPort 参数指定连接 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:None
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

-Name

指定断开连接的会话的友好名称。

Type:String[]
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

-ThrottleLimit

指定为运行此命令可建立的并发连接的最大数目。 如果省略此参数或输入 0 值,则使用默认值 32。

节流限制仅适用于当前命令,而不适用于会话或计算机。

Type:Int32
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

可以将会话 (PSSession) 管道传递给此 cmdlet。

输出

PSSession

此 cmdlet 返回一个 对象,该对象表示它重新连接到的会话。

备注

  • Connect-PSSession 仅重新连接到已断开连接的会话,即 State 属性值为 Disconnected 的会话。 只有连接到运行 Windows PowerShell 3.0 或更高版本的计算机或在其结束的会话才能断开连接并重新连接。

  • 如果在未断开连接的会话上使用 Connect-PSSession ,该命令不会影响会话,也不会生成错误。

  • 使用 EnableNetworkAccess 参数创建的具有交互式令牌的断开连接环回会话只能从创建会话的计算机重新连接。 此限制可使计算机免遭恶意访问。

  • PSSessionState 属性的值相对于当前会话。 因此,如果值为 Disconnected ,则表示 PSSession 未连接到当前会话。 但是,这并不意味着 PSSession 与所有会话断开连接。 它可能连接到另一个会话。 若要确定是否可以连接或重新连接到该会话,请使用 Availability 属性。

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

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

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

  • 连接到 PSSession 时,无法更改 PSSession 的空闲超时值。 Connect-PSSessionSessionOption 参数采用值为 IdleTimeoutSessionOption 对象。 但是,连接到 PSSession 时,将忽略 SessionOption 对象的 IdleTimeout 值和 $PSSessionOption 变量的 IdleTimeout 值。

    在创建 PSSession 时、使用 New-PSSessionInvoke-Command cmdlet 以及从 PSSession 断开连接时,可以设置和更改 PSSession 的空闲超时。

    PSSessionIdleTimeout 属性对于断开连接的会话至关重要,因为它确定断开连接的会话在远程计算机上保留多长时间。 断开连接的会话从其断开连接的时刻起就被视为空闲,即使命令正在断开连接的会话中运行也是如此。