从我的本地计算机在 Amazon EC2 实例上远程执行 powershell 脚本 [英] Execute powershell script remotely on Amazon EC2 instance from my local computer

查看:25
本文介绍了从我的本地计算机在 Amazon EC2 实例上远程执行 powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Amazon EC2 实例.

I have an Amazon EC2 instance.

在我的本地工作站上使用 powershell,我希望能够远程访问我的 Amazon EC2 实例并执行一些命令.

Using powershell on my local workstation, I want to be able to remote into my Amazon EC2 instance and execute some commands.

我在网上找到了很多文章,但都没有奏效,或者我误解了它们(可能是后者).

I have found many articles online but none are working or I misunderstood them (probably the latter).

我尝试过的一些是
使用 Powershell 远程管理 Windows EC2 实例
使用 Windows Powershell 管理 EC2 实例
启用- PSRemoting
如何在远程计算机上运行 PowerShell 命令

我的理解是我需要:

Amazon EC2 Dashboard > Network & Security > Security Groups > Add port 5985

//Local & EC2 PowerShell(Administrator)
enable-psremoting -force

//Local PowerShell(Administrator)
set-item wsman:localhostClientTrustedHosts -value "*" -force

$password = convertto-securestring -asplaintext -force -string myPassword
$credential = new-object -typename system.management.automation.pscredential -argumentlist "myUsername", $password
$session = new-pssession ec2-00-00-00-000.compute-1.amazonaws.com -credential $credential
enter-pssession $session  

但我收到此错误

new-pssession : [ec2-00-00-00-000.compute-1.amazonaws.com] Connecting to remote server
ec2-00-00-00-000.compute-1.amazonaws.com failed with the following error message : WinRM cannot complete the
operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and
that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM
firewall exception for public profiles limits access to remote computers within the same local subnet. For more
information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:12
+ $session = new-pssession ec2-00-00-00-000.compute-1.amazonaws.com -credential $c ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
   gTransportException
    + FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionOpenFailed

推荐答案

此处.

缺少的链接是(在 EC2 实例上)打开高级安全 Windows 防火墙并编辑入站规则.

The missing link was to (on the EC2 instance) open Windows Firewall with Advanced Security and edit an inbound rule.

完整步骤:

EC2 实例
1) 以管理员身份打开PowerShell
2) 输入 enable-psremoting -force
3) 打开高级安全 Windows 防火墙
4) 入站规则 -> 查找 Windows 远程管理 (Http-In) - 有 2 个,对两者都执行此操作
5) 右键 -> Properties -> Advanced -> 勾选public

EC2 Instance
1) Open PowerShell as administrator
2) Enter enable-psremoting -force
3) Open Windows Firewall with Advanced Security
4) Inbound Rules -> Find Windows Remote Management (Http-In) - there are 2, do this for both
5) Right click -> Properties -> Advanced -> Check public

本地
6) 以管理员身份打开PowerShell
7) 输入 enable-psremoting -force
8) 输入以下内容:

Local
6) Open PowerShell as administrator
7) Enter enable-psremoting -force
8) Enter the following:

$password = convertto-securestring -asplaintext -force -string MY_PASSWORD  
$credential = new-object -typename system.management.automation.pscredential -argumentlist "MY_USERNAME", $password  
$session = new-pssession MY_EC2_PUBLIC_IP -credential $credential  
enter-pssession $session  
Write-Host "Hello, World (from $env:COMPUTERNAME)"

这篇关于从我的本地计算机在 Amazon EC2 实例上远程执行 powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆