about_PSSessions

应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0

主题

about_PSSessions

简短说明

介绍 Windows PowerShell 会话 (PSSessions) 并说明如何建立到远程计算机的持续连接。

详细说明

若要运行远程计算机上的 Windows PowerShell 命令,你可以使用 cmdlet 的 ComputerName 参数,或者可以创建一个 Windows PowerShell 会话 (PSSession),然后在 PSSession 中运行命令。

当你创建 PSSession 时,Windows PowerShell 将建立与远程计算机的持续连接。使用 PSSession 运行远程计算机上的一系列相关命令。在相同的 PSSession 中运行的命令可以共享数据,比如变量、别名和函数的值。

你也可以在本地计算机上创建一个 PSSession 并在其中运行命令。本地 PSSession 使用 Windows PowerShell 远程处理基础结构来创建和维护 PSSession。

从 Windows PowerShell 3.0 开始,PSSession 独立于创建了它们的会话。在远程计算机(或位于远端或连接的“服务器端”的计算机)上维护活动 PSSession。因此,你可以断开与 PSSession 的连接,并在以后从同一台计算机或从另一台计算机重新与之连接。

本主题说明如何创建、使用、获取和删除 PSSession。有关更深入的信息,请参阅 about_PSSession_Details。

备注

PSSession 使用 Windows PowerShell 远程处理基础结构。若要使用 PSSession,必须配置本地计算机和远程计算机以进行远程处理。有关详细信息,请参阅 about_Remote_Requirements。

在 Windows Vista 以及更高版本的 Windows 中,若要在本地计算机上创建 PSSession,你必须使用“以管理员身份运行”选项启动 Windows PowerShell。

什么是会话?

会话是在其中运行 Windows PowerShell 的环境。

每次你启动 Windows PowerShell 时,就会为你创建一个会话,并且你可以在该会话中运行命令。你也可以向该会话添加项(比如模块和管理单元),你还可以创建项(比如变量、函数和别名)。这些项仅存在于该会话中,并且当会话结束时会删除这些项。

你也可以在本地计算机或远程计算机上创建用户托管的会话,这些会话称为“Windows PowerShell 会话”或 "PSSession"。与默认会话一样,你可以在 PSSession 中运行命令,还可以添加和创建项。

但是,与自动启动的会话不同,你可以控制你创建的 PSSession。你可以获取、创建、配置和删除它们,与之断开连接和重新与之连接,以及在同一个 PSSession 会话中运行多个命令。PSSession 将保持可用,直到被删除或超时为止。

通常,你创建一个 PSSession 以运行远程计算机上的一系列相关命令。当你在远程计算机上创建 PSSession 时,Windows PowerShell 将建立与远程计算机的持续连接以支持该会话。

如果你使用 Invoke-Command 的 ComputerName 参数或 Enter-PSSession cmdlet 来运行远程命令或启动一个交互式会话,Windows PowerShell 将在远程计算机上创建临时会话,并在命令完成后或在交互式会话结束后立即关闭该会话。你无法控制这些临时会话,并且你无法将其用于超过一个单个命令或单个交互式会话。

在 Windows PowerShell 中,“当前会话”是你正在其中工作的会话。“当前会话”可以指任何会话,包括临时会话或 PSSession。

为什么使用 PSSESSION?

当你需要与远程计算机的持续连接时,则使用 PSSession。通过 PSSession,你可以运行可共享数据的一系列命令,比如变量的值、函数的内容或别名的定义。

你可以在没有创建 PSSession 的情况下运行远程命令。使用 remote-enabled cmdlet 的 ComputerName 参数在一台或多台计算机上运行单个命令或一系列不相关的命令。

当使用 Invoke-Command 的 ComputerName 参数或 Enter-PSSession cmdlet 时,Windows PowerShell 会建立与远程计算机的临时连接,然后在命令完成后立即关闭该连接。当关闭连接后,任何你创建的数据元素都将会丢失。

其他具有 ComputerName 参数的 cmdlet(比如 Get-Eventlog 和 Get-WmiObject)使用不同的远程处理技术来收集数据。没有其他程序像 PSSession 一样创建持续连接。

如何创建 PSSESSION

若要创建 PSSession,请使用 New-PSSession cmdlet。若要在远程计算机上创建 PSSession,请使用 New-PSSession cmdlet 的 ComputerName 参数。

例如,以下命令在 Server01 计算机上创建新 PSSession。

        New-PSSession -ComputerName Server01

当你提交该命令时,New-PSSession 将创建 PSSession 并返回一个表示 PSSession 的对象。当你创建 PSSession 时,你可以将对象保存在变量中,或者你可以使用 Get-PSSession 命令在稍后获取 PSSession。

例如,以下命令在 Server01 计算机上创建新 PSSession,并将生成的对象保存在 $ps 变量中。

        $ps = New-PSSession -ComputerName Server01

如何在多台计算机上创建 PSSESSION

要在多台计算机上创建 PSSession,则使用 New-PSSession cmdlet 的 ComputerName 参数。在以逗号分隔的列表中键入远程计算机的名称。

例如,若要在 Server01、Server02 和 Server03 计算机上创建 PSSession,请键入:

        New-PSSession -ComputerName Server01, Server02, Server03

New-PSSession 会在每台远程计算机上创建一个 PSSession。

如何获取 PSSESSION

要获取在当前会话中创建的 PSSession,则使用不带 ComputerName 参数的 Get-PSSession cmdlet。Get-PSSession 返回与 New-PSSession 返回的对象相同类型的对象。在此处插入部分正文。

以下命令获取在当前会话中创建的所有 PSSession。

        Get-PSSession

PSSession 的默认显示显示其 ID 和一个默认显示名称。当你创建该会话时,你可以分配一个备用显示名称。

        Id   Name       ComputerName    State    ConfigurationName
        ---  ----       ------------    -----    ---------------------
        1    Session1   Server01        Opened   Microsoft.PowerShell
        2    Session2   Server02        Opened   Microsoft.PowerShell
        3    Session3   Server03        Opened   Microsoft.PowerShell

你还可以将 PSSession 保存在变量中。以下命令获取 PSSession 并将其保存在 $ps123 变量中。

        $ps123 = Get-PSSession

使用 PSSession cmdlet 时,你可以通过其 ID、名称或其实例 ID (GUID) 来引用 PSSession。以下命令通过 PSSession 的 ID 获取 PSSession 并将其保存在 $ps01 变量中。

        $ps01 = Get-PSSession -Id 1

从 Windows PowerShell 3.0 开始,PSSession 保留在远程计算机上。若要获取你在特定远程计算机上创建的 PSSession,请使用 Get-PSSession cmdlet 的 ComputerName 参数。以下命令获取你在 Server01 远程计算机上创建的 PSSession。这包括在本地计算机或其他计算机上的当前会话和其他会话中创建的 PSSession。

        Get-PSSession -ComputerName Server01

在 Windows PowerShell 2.0 中,Get-PSSession 只获取在当前会话中创建的 PSSession。它不获取在其他会话中或在其他计算机上创建的 PSSession,即使这些会话已连接到本地计算机并且在本地计算机上运行命令。

如何在 PSSESSION 中运行命令

若要在一个或多个 PSSession 中运行命令,请使用 Invoke-Command cmdlet。使用 Session 参数指定 PSSession,使用 ScriptBlock 参数指定命令。

例如,若要在保存在 $ps123 变量中的 3 个 PSSession 的每一个中运行 Get-ChildItem ("dir") 命令,则键入:

        Invoke-Command -Session $ps123 -ScriptBlock {Get-ChildItem}

如何删除 PSSESSION

结束使用 PSSession 后,使用 Remove-PSSession cmdlet 删除该 PSSession 并释放其正在使用的资源。

        Remove-PSSession -Session $ps

- 或者-

        Remove-PSSession -Id 1

若要从远程计算机上删除 PSSession,请使用 Remove-PSSession cmdlet 的 ComputerName 参数。

       Remove-PSSession -ComputerName Server01 -Id 1

如果你不删除该 PSSession,它将保持可供使用直到超时。

你还可以使用 New-PSSessionOption cmdlet 的 IdleTimeout 参数来为空闲 PSSession 设置过期时间。有关详细信息,请参阅 New-PSSessionOption。

PSSESSION CMDLET

    Cmdlet                Description
    -----------------     ------------------------------------------------------
    New-PSSession         Creates a new PSSession on a local or remote computer.

    Get-PSSession         Gets the PSSessions in the current session.

    Remove-PSSession      Deletes the PSSessions in the current session.

    Enter-PSSession       Starts an interactive session.

    Exit-PSSession        Ends an interactive session.
    
    Disconnect-PSSession  Disconnects a PSSession from the current session.

    Connect-PSSession     Connects a PSSession to the current session.
     
    Receive-PSSession     Gets the results of commands that ran in a disconnected
                          session.

有关 PSSession cmdlet 的列表,请键入:

       get-help *-PSSession

有关详细信息

有关 PSSession 的详细信息,请参阅 about_PSSession_Details。

另请参阅

about_Remote

about_Remote_Disconnected_Sessions

about_Remote_Requirements

Connect-PSSession

Disconnect-PSSession

Enter-PSSession

Exit-PSSession

Get-PSSession

Invoke-Command

New-PSSession

Remove-PSSession