当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

作者:精品下载站 日期:2024-12-14 22:59:30 浏览:14 分类:玩电脑

使用 PowerShell Invoke-Command 在远程计算机上运行脚本


在本文中,我们将学习如何使用 Invoke-Command cmdlet 远程运行 PowerShell 命令或脚本。您可以使用 PowerShell 在网络中的一台或多台计算机上远程运行命令。 Invoke-Command cmdlet 使用 PowerShell Remoting 的远程管理功能。 PowerShell 远程处理允许您通过 WinRM(Windows 远程管理)服务和Web 管理服务(WS-Management)协议远程连接到计算机上的 PowerShell 会话。此服务提供建立远程 PowerShell 会话并运行代码的能力。

为 PowerShell 远程处理配置 WinRM

PowerShell Remoting 使用 HTTP(端口 TCP/5985)或 HTTPS(端口 TCP/5986)在计算机之间进行通信。默认情况下,使用 HTTP 协议,但即使此流量也是使用 AES-56 加密的(但是,存在中间人类型攻击的威胁)。也可以使用 Kerberos 或 NTLM 身份验证。

WinRM 必须在要连接的远程计算机上运行。检查WinRM服务状态:

Get-Service -Name "*WinRM*" | fl

如果服务没有运行,启动它:

Enable-PSRemoting
WinRM has been updated to receive requests.
WinRM service started.
WinRM is already set up for remote management on this computer.

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

此命令将启动 WinRM 服务(并将其设置为自动启动)、设置默认 winrm 设置并向 Windows 防火墙添加例外规则。这

Enable-PSRemoting -Force

命令启用 WinRM,而不提示用户。

然后,您可以使用 PowerShell Remoting 远程连接到计算机。

请注意,如果您的网络类型设置为公共,则默认情况下 PowerShell 远程处理将无法工作。然后该命令返回以下错误:

Set-WSManQuickConfig : ... WinRM firewall exception will not work since one of the network connection types on this machine is set to Public. Change the network connection type to either Domain or Private and try again.

您必须将网络位置更改为专用或使用以下命令:

Enable-PSRemoting -SkipNetworkProfileCheck.

还启用允许在公共网络中访问 WinRM 的 Windows Defender 防火墙规则。您可以使用 GPO 或 PowerShell 启用防火墙规则:

Set-NetFirewallRule -Name 'WINRM-HTTP-In-TCP' -RemoteAddress Any

要通过 PowerShell Remoting 测试与远程计算机的连接,请运行以下命令:

Test-WsMan compname1

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

如果您没有 Active Directory 域或者通过 IP 地址通过 PowerShell Remoting 访问计算机,则在这种情况下将使用 NTLM 协议进行身份验证。使用 NTLM 时,如果尝试运行 Invoke-Command,则会出现以下错误:

[192.168.1.201] Connecting to remote server 192.168.1.102 failed with the following error message: The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: thetransport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. + FullyQualifiedErrorId: CannotUseIPAddress,PSSessionStateBroken

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

要使 NTLM 身份验证在您用于连接的计算机上正常工作,请执行更多操作:为 WinRM 颁发 SSL 证书或将主机名/IP 地址添加到受信任的主机列表:

Set-Item wsman:\localhost\Client\TrustedHosts -value 192.168.1.201

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

或者您可以允许连接到所有计算机(不推荐,因为这是 NTLM 的缺点之一 - 它不支持相互身份验证)。

Set-Item wsman:\localhost\Client\TrustedHosts -value *

必须在远程主机上应用相同的设置。

要显示受信任主机的列表,请运行以下命令:

Get-Item WSMan:\localhost\Client\TrustedHosts

要应用更改,请重新启动 WinRM:

Restart-Service WinRM

您还可以使用组策略启用和配置 WinRM。

如何使用 Invoke-Command 远程运行 PowerShell 命令?

Invoke-Command cmdlet 允许在一台或多台远程计算机上运行命令。

例如,要在远程计算机上运行单个命令,请使用以下命令:

Invoke-Command -ComputerName dc01 -ScriptBlock {$PSVersionTable.PSVersion}

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

此命令将显示远程计算机上安装的 PowerShell 版本,其名称在

-ComputerName

范围。在远程计算机上输入要运行的命令

-ScriptBlock {[cmdlet]}

堵塞。

默认情况下,通过 Invoke-Command 发送的命令将以远程计算机上的当前用户身份执行。如果您想以其他用户身份运行它,请请求用户凭据并将其保存到变量中:

$cred = Get-Credential
Invoke-Command -ComputerName dc01 -Credential $cred -ScriptBlock {Get-NetAdapter}

此 PowerShell 命令显示远程计算机上的网络接口列表:

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

您可以在 ScriptBlock 中输入多个命令,以分号分隔。例如,以下命令将显示当前时区并将其更改为另一时区:

Invoke-Command -Computername dc01 -ScriptBlock {Get-TimeZone| select DisplayName;Set-TimeZone -Name "Central Europe Standard Time”}

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

Invoke-Command 不仅允许您运行单个命令,还允许您运行 PowerShell 脚本。为此,使用 -FilePath 参数(而不是 -ScriptBlock)。在这种情况下,您指定计算机上本地 PS1 脚本文件的路径(不需要将脚本文件复制到目标远程计算机):

Invoke-Command -ComputerName DC01 -FilePath C:\PS\Scripts\CheckSMBversion.ps1

如何使用Invoke-Command在多台计算机上同时运行命令?

您可以使用 Invoke-Command 在多台远程计算机上并行(同时)运行命令。

在最简单的情况下,运行 PowerShell 命令的计算机的名称用逗号分隔:

Invoke-Command server1, server2, server3 -ScriptBlock {get-date}

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

您可以将计算机列表放入变量(数组)中:

$servers = @("server1","server2","server3")
Invoke-Command -ScriptBlock { get-date} -ComputerName $servers

或者从文本文件中获取:

Invoke-Command -ScriptBlock {Restart-Service spooler} -ComputerName(Get-Content c:\ps\servers.txt)

您还可以使用 PowerShell 模块的 AD 中的 Get-ADComputer cmdlet 获取 AD 中的计算机列表:

要在域中的所有 Windows Server 主机中运行命令,请使用以下 PowerShell 代码:

$computers = (Get-ADComputer -Filter 'OperatingSystem -like "*Windows server*" -and Enabled -eq "true"').Name
Invoke-Command -ComputerName $computers -ScriptBlock {Get-Date} -ErrorAction SilentlyContinue

如果计算机关闭或不可用,脚本不会因 SilentlyContinue 参数而停止,并将继续在其他计算机上运行。

要了解结果来自哪台计算机,请使用 PSComputerNamee 环境变量。

$results = Invoke-Command server1, server2, server3 -ScriptBlock {get-date}
$results | Select-Object PSComputerName, DateTime

[玩转系统] 使用 PowerShell Invoke-Command 在远程计算机上运行脚本

当在多台计算机上使用 Invoke-Command 运行命令时,该命令会同时运行。 Invoke-Command 对同时管理的最大计算机数量有限制(同时 PSSession 的数量有限)。此限制在 ThrottleLimit 参数中设置(默认值为 32)。如果要在超过 32 台计算机(例如 128 台)上运行命令,请使用

-ThrottleLimit 128

(但是,您的计算机将有更高的负载来建立大量 PSSession)。

要在后台通过 Invoke-Command 在远程计算机上运行命令,需要一个特殊属性

-AsJob

用来。那么命令的结果不会返回到控制台。要获取结果,请使用 Receive-Job cmdlet。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯