主题
    about_Remote

简短说明
    说明如何在 Windows PowerShell 中运行远程命令。


详细说明
    可以使用临时或持续性连接在单台计算机或多台计算机上运行远程命令。也可启动与单台远程计算机的交互式会话。
    
    本主题提供了一系列示例,以说明如何运行不同类型的远程命令。在尝试这些基本命令之后,请阅读介绍在
    这些命令中使用的每个 cmdlet 的帮助主题。这些主题提供了相关详细信息,并说明了如何修改命令以满
    足您的需要。

    注意:若要使用 Windows PowerShell 远程处理,必须为本地和远程计算机配置远程处理。有关详细信
    息,请参阅 about_Remote_Requirements。
          

 如何启动交互式会话 (ENTER-PSSESSION)

     运行远程命令的最简单方式是启动与远程计算机的交互式会话。

     启动会话时,键入的命令会在远程计算机上运行,就像直接在远程计算机上键入这些命令一样。
     在每次交互式会话中,只能连接到一台计算机。

     若要启动交互式会话,请使用 Enter-PSSession cmdlet。以下命令可启动与 Server01 计算机的交
     互式会话:

	enter-pssession server01

     命令提示符会更改以指示已连接到 Server01 计算机。

	Server01\PS>

     现在,您可以在 Server01 计算机上键入命令。

     若要终止交互式会话,请键入:

	exit-pssession


     有关详细信息,请参阅 Enter-PSSession。

      

 如何使用具有 ComputerName 参数的 cmdlet 获取远程数据

    有几个 cmdlet 具有 ComputerName 参数,您可通过该参数从远程计算机获取对象。

    由于这些 cmdlet 不使用基于 WS 管理的 Windows PowerShell 远程功能,因此可在任何运行 Windows 
    PowerShell 的计算机上使用这些 cmdlet 的 ComputerName 参数。
    无需为这些计算机配置 Windows PowerShell 远程处理,而且这些计算机也不一定要满足远程处理的系
    统要求。

    以下 cmdlet 具有 ComputerName 参数:
 
        Clear-EventLog    Limit-EventLog
        Get-Counter       New-EventLog
        Get-EventLog      Remove-EventLog
        Get-HotFix        Restart-Computer
        Get-Process       Show-EventLog
        Get-Service       Show-Service
        Get-WinEvent      Stop-Computer
        Get-WmiObject     Write-EventLog

  
    例如,以下命令可以获取 Server01 远程计算机上的服务:

	get-service -computername server01

      
    通常,不经过特殊配置即可支持远程功能的 cmdlet 都具有 ComputerName 参数,
    而没有 Session 参数。要在您的会话中查找这些 cmdlet,请键入: 

       get-command | where { $_.parameters.keys -contains "ComputerName" -and $_.parameters.keys -notcontains "Session"}


 如何运行远程命令

    若要在远程计算机上运行其他命令,请使用 Invoke-Command cmdlet。

    若要运行单个命令或几个不相关的命令,请使用 Invoke-Command 的 ComputerName 参数来指定远程
    计算机。请使用 ScriptBlock 参数来指定命令。

    例如,以下命令在 Server01 计算机上运行 Get-Culture 命令。

	invoke-command -computername Server01 -scriptblock {get-culture}

    ComputerName 参数适用于在一台或多台计算机上运行单个命令或多个不相关命令的情况。
    若要建立与远程计算机的持续性连接,请使用 Session 参数。

 
 如何创建持续性连接 (PSSESSION)

    使用 Invoke-Command cmdlet 的 ComputerName 参数时,Windows PowerShell 将只为该命令
    建立连接。然后,当该命令完成后,Windows PowerShell 会关闭该连接。在该命令中定义的所有变量或
    函数都将丢失。

    若要创建与远程计算机的持续性连接,请使用 New-PSSession cmdlet。例如,以下命令在 Server01 
    和 Server02 计算机上创建 PSSession,然后将 PSSession 保存在 $s 变量中。

	$s = new-pssession -computername Server01, Server02


 如何在 PSSession 中运行命令
  
    通过 PSSession 可以运行一系列共享数据(如函数、别名和变量值)的远程命令。
    若要在 PSSession 中运行命令,请使用 Invoke-Command cmdlet 的 Session 参数。

    例如,以下命令使用 Invoke-Command cmdlet 在 Server01 和 Server02 计算机上的 
    PSSession 中运行 Get-Process 命令。该命令将进程保存在每个 PSSession 的 $p 变量中。

        invoke-command -session $s -scriptblock {$p = get-process}

    由于 PSSession 使用持续性连接,因此可在使用 $p 变量的同一个 PSSession 中运行其他命令。
    以下命令对保存在 $p 中的进程数量进行计数。

        invoke-command -session $s -scriptblock {$p.count}

 如何在多台计算机上运行远程命令

    若要在多台计算机上运行某个远程命令,请在 Invoke-Command 的 ComputerName 参数值中键入所有
    计算机名称。请使用逗号将命令分隔开。

    例如,以下命令在三台计算机上运行 Get-Culture 命令:

	invoke-command -computername S1, S2, S3 -scriptblock {get-culture}

    还可在多个 PSSession 中运行某个命令。以下命令在 Server01、Server02 和 Server03 计算机
    上创建 PSSession,然后在每个 PSSession 中运行 Get-Culture 命令。

        $s = new-pssession -computername S1, S2, S3
	invoke-command -session $s -scriptblock {get-culture}


    若要包括计算机的本地计算机列表,请键入本地计算机的名称,并键入一个圆点 (.),或键入
    "localhost"。

	invoke-command -computername S1, S2, S3, localhost -scriptblock {get-culture}


 如何在远程计算机上运行脚本

    若要在远程计算机上运行本地脚本,请使用 Invoke-Command 的 FilePath 参数。

    例如,以下命令在 S1 和 S2 计算机上运行 Sample.ps1 脚本:

        invoke-command -computername S1, S2 -filepath C:\Test\Sample.ps1

    脚本的结果将返回到本地计算机。无需复制任何文件。
  

 如何停止远程命令

   若要中断命令,请按 Ctrl+C。中断请求将传递给远程计算机,在该计算机上终止远程命令。



有关详细信息

    -- 有关远程处理的系统要求的信息,请参阅 about_Remote_Requirements。

    -- 有关远程输出格式设置的帮助,请参阅 about_Remote_Output。

    -- 有关远程处理的工作方式、远程数据管理方法、特殊配置、安全问题和其他常见问题的信息, 
       请参阅 about_Remote_FAQ。

    -- 有关解决远程处理错误的帮助,请参阅 about_Remote_Troubleshooting。

    -- 有关 PSSession 和持续性连接的信息,请参阅 about_PSSessions。

    -- 有关 Windows PowerShell 后台作业的信息,请参阅 about_Jobs。

关键字
    about_Remoting

另请参阅 
    about_PSSessions 
    about_Remote_Requirements 
    about_Remote_FAQ 
    about_Remote_TroubleShooting 
    Enter-PSSession  
    Invoke-Command 
    New-PSSession 




目录