power shell远程_运行远程命令 - PowerShell | Microsoft Docs

运行远程命令Running Remote Commands

08/21/2020

本文内容

可以使用单个 PowerShell 命令在一台或数百台计算机上运行命令。You can run commands on one or hundreds of computers with a single PowerShell command. Windows PowerShell 通过使用各种技术(包括 WMI、RPC 和 WS-Management)支持远程计算。Windows PowerShell supports remote computing by using various technologies, including WMI, RPC, and WS-Management.

PowerShell Core 支持 WMI、WS-Management 和 SSH 远程处理。PowerShell Core supports WMI, WS-Management, and SSH remoting. 在 PowerShell 6 中,RPC 不再受支持。In PowerShell 6, RPC is no longer supported. 在 PowerShell 7 及更高版本中,RPC 仅在 Windows 中受支持。In PowerShell 7 and above, RPC is supported only in Windows.

有关在 PowerShell Core 中进行远程处理的详细信息,请参阅以下文章:For more information about remoting in PowerShell Core, see the following articles:

无需配置即可进行 Windows PowerShell 远程处理Windows PowerShell Remoting Without Configuration

许多 Windows PowerShell cmdlet 都具有 ComputerName 参数,此参数可使你在一台或多台远程计算机上收集数据和更改设置。Many Windows PowerShell cmdlets have the ComputerName parameter that enables you to collect data and change settings on one or more remote computers. 这些 cmdlet 使用不同的通信协议,无需进行任何特殊配置即可在所有 Windows 操作系统上工作。These cmdlets use varying communication protocols and work on all Windows operating systems without any special configuration.

这些 cmdlet 包括:These cmdlets include:

通常情况下,支持无需特殊配置即可进行远程处理的 cmdlet 具有 ComputerName 参数,但不具有 Session 参数。Typically, cmdlets that support remoting without special configuration have the ComputerName parameter and don't have the Session parameter. 若要在会话中查找这些 cmdlet,请键入:To find these cmdlets in your session, type:

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

Windows PowerShell 远程处理Windows PowerShell Remoting

使用 WS-Management 协议,Windows PowerShell 远程处理使你可以在一台或多台远程计算机上运行任何 Windows PowerShell 命令。Using the WS-Management protocol, Windows PowerShell remoting lets you run any Windows PowerShell command on one or more remote computers. 你可以建立持久连接、启动交互会话并在远程计算机上运行脚本。You can establish persistent connections, start interactive sessions, and run scripts on remote computers.

若要使用 Windows PowerShell 远程处理,必须配置远程计算机以进行远程管理。To use Windows PowerShell remoting, the remote computer must be configured for remote management.

有关详细信息(包括说明),请参阅关于远程要求。For more information, including instructions, see About Remote Requirements.

一旦配置了 Windows PowerShell 远程处理,就会有许多远程处理策略可供你使用。Once you have configured Windows PowerShell remoting, many remoting strategies are available to you.

本文只列出了其中的几个策略。This article lists just a few of them. 有关详细信息,请参阅关于远程。For more information, see About Remote.

启动交互会话Start an Interactive Session

若要使用单台远程计算机启动交互会话,请使用 Enter-PSSession cmdlet。To start an interactive session with a single remote computer, use the Enter-PSSession cmdlet. 例如,若要使用 Server01 远程计算器启动交互会话,请键入:For example, to start an interactive session with the Server01 remote computer, type:

Enter-PSSession Server01

命令提示符更改为显示远程计算机的名称。The command prompt changes to display the name of the remote computer. 你在提示符中键入的任何命令都将在远程计算机上运行,并且结果将显示在本地计算机上。Any commands that you type at the prompt run on the remote computer and the results are displayed on the local computer.

若要结束交互会话,请键入:To end the interactive session, type:

Exit-PSSession

有关 Enter-PSSession 和 Exit-PSSession cmdlet 的详细信息,请参阅:For more information about the Enter-PSSession and Exit-PSSession cmdlets, see:

运行远程命令Run a Remote Command

若要在一台或多台计算机上运行一个命令,请使用 Invoke-Command cmdlet。To run a command on one or more computers, use the Invoke-Command cmdlet. 例如,若要在 Server01 和 Server02 远程计算机上运行 Get-UICulture 命令,请键入:For example, to run a Get-UICulture command on the Server01 and Server02 remote computers, type:

Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {Get-UICulture}

输出将返回到你的计算机。The output is returned to your computer.

LCID Name DisplayName PSComputerName

---- ---- ----------- --------------

1033 en-US English (United States) server01.corp.fabrikam.com

1033 en-US English (United States) server02.corp.fabrikam.com

运行脚本Run a Script

若要在一台或多台远程计算机上运行脚本,请使用 Invoke-Command cmdlet 的 FilePath 参数。To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. 该脚本必须在你的本地计算机上或可由其访问。The script must be on or accessible to your local computer. 结果将返回到你的本地计算机。The results are returned to your local computer.

例如,以下命令在远程计算机 Server01 和 Server02 上运行 DiskCollect.ps1 脚本。For example, the following command runs the DiskCollect.ps1 script on the remote computers, Server01 and Server02.

Invoke-Command -ComputerName Server01, Server02 -FilePath c:\Scripts\DiskCollect.ps1

建立持久连接Establish a Persistent Connection

使用 New-PSSession cmdlet 在远程计算机上创建一个持久会话。Use the New-PSSession cmdlet to create a persistent session on a remote computer. 以下示例在 Server01 和 Server02 上创建远程会话。The following example creates remote sessions on Server01 and Server02. 会话对象存储在 $s 变量中。The session objects are stored in the $s variable.

$s = New-PSSession -ComputerName Server01, Server02

建立会话后,你可以在这些会话中运行任何命令。Now that the sessions are established, you can run any command in them. 此外,由于会话是持久的,因此你可以从一个命令收集数据,并在另一个命令中使用它。And because the sessions are persistent, you can collect data from one command and use it in another command.

例如,下面的命令在 $s 变量中的会话中运行 Get-Hotfix 命令,并且它将结果保存在 $h 变量中。For example, the following command runs a Get-HotFix command in the sessions in the $s variable and it saves the results in the $h variable. 将在 $s 中的每个会话中创建 $h 变量,但它不会存在于本地会话中。The $h variable is created in each of the sessions in $s, but it doesn't exist in the local session.

Invoke-Command -Session $s {$h = Get-HotFix}

现在,可以将 $h 变量中的数据与同一会话中的其他命令一起使用。Now you can use the data in the $h variable with other commands in the same session. 结果将显示在本地计算机上。The results are displayed on the local computer. 例如:For example:

Invoke-Command -Session $s {$h | where {$_.InstalledBy -ne "NTAUTHORITY\SYSTEM"}}

高级远程处理Advanced Remoting

Windows PowerShell 远程管理就在此处开始。Windows PowerShell remote management just begins here. 通过使用随 Windows PowerShell 一起安装的 cmdlet,你可以从本地和远程端点建立和配置远程会话、创建自定义和受限制的会话、允许用户从实际在远程会话上隐式运行的远程会话中导入命令、配置远程会话的安全性等。By using the cmdlets installed with Windows PowerShell, you can establish and configure remote sessions both from the local and remote ends, create customized and restricted sessions, allow users to import commands from a remote session that actually run implicitly on the remote session, configure the security of a remote session, and much more.

Windows PowerShell 包含一个 WSMan 提供程序。Windows PowerShell includes a WSMan provider. 提供程序创建 WSMAN: 驱动器,允许你在本地计算机和远程计算机上的配置设置层次结构之间导航。The provider creates a WSMAN: drive that lets you navigate through a hierarchy of configuration settings on the local computer and remote computers.

有关 WSMan 提供程序的详细信息,请参阅 WSMan 提供程序和关于 WS-Management Cmdlet,或在 Windows PowerShell 控制台中键入 Get-Help wsman。For more information about the WSMan provider, see WSMan Provider and About WS-Management Cmdlets, or in the Windows PowerShell console, type Get-Help wsman.

有关详细信息,请参阅:For more information, see:

另请参阅See Also

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值