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

PowerShell Remoting lets you run PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.

使用PowerShell Remoting,您可以在远程Windows系统上运行PowerShell命令或访问完整的PowerShell会话。 它类似于SSH,用于访问其他操作系统上的远程终端。

PowerShell is locked-down by default, so you’ll have to enable PowerShell Remoting before using it. This setup process is a bit more complex if you’re using a workgroup instead of a domain—for example, on a home network—but we’ll walk you through it.

默认情况下,PowerShell处于锁定状态,因此您必须先启用PowerShell Remoting。 如果您使用工作组而不是域(例如,在家庭网络中),则此设置过程会更加复杂,但是我们将逐步指导您。

在要远程访问的PC上启用PowerShell远程处理 (Enable PowerShell Remoting on the PC You Want to Access Remotely)

Your first step is to enable PowerShell Remoting on the PC to which you want to make remote connections. On that PC, you’ll need to open PowerShell with administrative privileges.

第一步是在要建立远程连接的PC上启用PowerShell Remoting。 在该PC上,您需要使用管理权限打开PowerShell。

In Windows 10, press Windows+X and then choose PowerShell (Admin) from the Power User menu.

在Windows 10中,按Windows + X,然后从“高级用户”菜单中选择PowerShell(Admin)。

In Windows 7 or 8, hit Start, and then type “powershell.” Right-click the result and choose “Run as administrator.”

在Windows 7或8中,单击“开始”,然后键入“ powershell”。 右键单击结果,然后选择“以管理员身份运行”。

In the PowerShell window, type the following cmdlet (PowerShell’s name for a command), and then hit Enter:

在PowerShell窗口中,键入以下cmdlet(命令的PowerShell名称),然后按Enter:

Enable-PSRemoting -Force

This command starts the WinRM service, sets it to start automatically with your system, and creates a firewall rule that allows incoming connections. The -Force part of the cmdlet tells PowerShell to perform these actions without prompting you for each step.

此命令启动WinRM服务,将其设置为与系统一起自动启动,并创建允许传入连接的防火墙规则。 cmdlet的-Force部分告诉PowerShell执行这些操作,而不提示您执行每个步骤。

If your PCs are part of a domain, that’s all the setup you have to do. You can skip on ahead to testing your connection. If your computers are part of a workgroup—which they probably are on a home or small business network—you have a bit more setup work to do.

如果您的PC是域的一部分,那就是您要做的所有设置。 您可以跳过以测试您的连接。 如果您的计算机是工作组的一部分(它们可能位于家庭或小型企业网络中),则您还有更多的设置工作要做。

Note: Your success in setting up remoting in a domain environment depends entirely on your network’s setup. Remoting might be disabled—or even enabled—automatically by group policy configured by an admin. You might also not have the permissions you need to run PowerShell as an administrator. As always, check with your admins before you try anything like this. They might have good reasons for not allowing the practice, or they might be willing to set it up for you.

注意:在域环境中成功设置远程处理完全取决于网络的设置。 远程处理可能会被管理员配置的组策略自动禁用,甚至启用。 您可能也没有以管理员身份运行PowerShell所需的权限。 与往常一样,在尝试这种操作之前,请先咨询管理员。 他们可能有充分的理由不允许这样做,或者他们可能愿意为您进行设置。

设置工作组 (Set Up Your Workgroup)

If your computers aren’t on a domain, you need to perform a few more steps to get things set up. You should have already enabled Remoting on the PC to which you want to connect, as we described in the previous section.

如果您的计算机不在域中,则需要执行一些其他步骤来进行设置。 如上一节所述,您应该已经在要连接的PC上启用了远程处理。

Note: For PowerShell Remoting to work in a workgroup environment, you must configure your network as a private, not public, network. For more on the difference—and how to change to a private network if you already have a public network set up—check out our guide on private vs. public networks.

注意:为了使PowerShell Remoting在工作组环境中工作,您必须将网络配置为私有而不是公共网络。 有关差异的更多信息,以及如何在已建立公共网络的情况下更改为专用网络的信息,请查看我们的专用和公共网络指南

Next, you need to configure the TrustedHosts setting on both the PC to which you want to connect and the PC (or PCs) you want to connect from, so the computers will trust each other. You can do this in one of two ways.

接下来,您需要在要连接的PC要从其连接的PC上都配置TrustedHosts设置,以便计算机之间可以相互信任。 您可以通过以下两种方式之一来执行此操作。

If you’re on a home network where you want to go ahead and trust any PC to connect remotely, you can type the following cmdlet in PowerShell (again, you’ll need to run it as Administrator).

如果您要在家庭网络上继续前进并信任任何PC进行远程连接,则可以在PowerShell中键入以下cmdlet(同样,您需要以管理员身份运行它)。

Set-Item wsman:\localhost\client\trustedhosts *

The asterisk is a wildcard symbol for all PCs. If instead you want to restrict computers that can connect, you can replace the asterisk with a comma-separated list of IP addresses or computer names for approved PCs.

星号是所有PC的通配符。 相反,如果您想限制可以连接的计算机,则可以用逗号分隔的IP地址列表或认可的PC的计算机名称替换星号。

After running that command, you’ll need to restart the WinRM service so your new settings take effect. Type the following cmdlet and then hit Enter:

运行该命令后,您需要重新启动WinRM服务,以便新设置生效。 键入以下cmdlet,然后按Enter:

Restart-Service WinRM

And remember, you’ll need to run those two cmdlets on the PC to which you want to connect, as well as on any PCs you want to connect from.

请记住,您需要在要连接的PC以及要从其连接的任何PC上运行这两个cmdlet。

测试连接 (Test the Connection)

Now that you’ve got your PCs set up for PowerShell Remoting, it’s time to test the connection. On the PC you want to access the remote system from, type the following cmdlet into PowerShell (replacing “COMPUTER” with the name or IP address of the remote PC), and then hit Enter:

现在您已经为PowerShell Remoting设置了PC,现在该测试连接了。 在要从其访问远程系统的PC上,在PowerShell中键入以下cmdlet(将“ COMPUTER”替换为远程PC的名称或IP地址),然后按Enter:

Test-WsMan COMPUTER

This simple command tests whether the WinRM service is running on the remote PC. If it completes successfully, you’ll see information about the remote computer’s WinRM service in the window—signifying that WinRM is enabled and your PC can communicate. If the command fails, you’ll see an error message instead.

这个简单的命令测试WinRM服务是否在远程PC上运行。 如果成功完成,您将在窗口中看到有关远程计算机的WinRM服务的信息-表示已启用WinRM并且您的PC可以通信。 如果命令失败,则会显示一条错误消息。

执行一个远程命令 (Execute a Single Remote Command)

To run a command on the remote system, use the Invoke-Command cmdlet using the following syntax:

若要在远程系统上运行命令,请使用以下语法使用Invoke-Command cmdlet:

Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME

“COMPUTER” represents the remote PC’s name or IP address. “COMMAND” is the command you want to run. “USERNAME” is the username you want to run the command as on the remote computer. You’ll be prompted to enter a password for the username.

“计算机”代表远程PC的名称或IP地址。 “ COMMAND”是您要运行的命令。 “ USERNAME”是您要在远程计算机上运行命令的用户名。 系统将提示您输入用户名的密码。

Here’s an example. I want to view the contents of the C:\ directory on a remote computer with the IP address 10.0.0.22. I want to use the username “wjgle,” so I would use the following command:

这是一个例子。 我想查看IP地址为10.0.0.22的远程计算机上C:\目录的内容。 我想使用用户名“ wjgle”,所以我将使用以下命令:

Invoke-Command -ComputerName 10.0.0.22 -ScriptBlock { Get-ChildItem C:\ } -credential wjgle

开始远程会话 (Start a Remote Session)

If you have several cmdlets you want to run on the remote PC, instead of repeatedly typing the Invoke-Command cmdlet and the remote IP address, you can start a remote session instead. Just type the following cmdlet and then hit Enter:

如果您要在远程PC上运行多个cmdlet,而不必重复键入Invoke-Command cmdlet和远程IP地址,则可以启动远程会话。 只需键入以下cmdlet,然后按Enter:

Enter-PSSession -ComputerName COMPUTER -Credential USER

Again, replace “COMPUTER” with the name or IP address of the remote PC and replace “USER” with the name of the user account you want to invoke.

同样,将“ COMPUTER”替换为远程PC的名称或IP地址,并将“ USER”替换为要调用的用户帐户的名称。

Your prompt changes to indicate the remote computer to which you’re connected, and you can execute any number of PowerShell cmdlets directly on the remote system.

提示符更改为指示您连接到的远程计算机,并且可以直接在远程系统上执行任意数量的PowerShell cmdlet。

翻译自: https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值