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

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

问题描述

我有一个Amazon EC2实例。



在本地工作站上使用powershell,我希望能够远程进入我的Amazon EC2实例并执行一些命令。 / p>

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



有些我试过了

使用Powershell远程管理Windows EC2实例

使用Windows Powershell管理EC2实例

启用-PSRemoting

如何在Remot上运行PowerShell命令电子计算机



我的理解是我需要:

  Amazon EC2仪表板>网络与安全>安全组>添加端口5985 

//本地& EC2 PowerShell(管理员)
enable-psremoting -force

//本地PowerShell(管理员)
set-item wsman:\localhost\Client\TrustedHosts -value * -force

$ password = convertto-securestring -asplaintext -force -string myPassword
$ credential =新对象-typename system.management.automation.pscredential-参数列表 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]连接到远程服务器
ec2-00-00-00-000.compute-1.amazonaws.com失败,以下错误消息:WinRM无法完成
操作。验证指定的计算机名称有效,可通过网络访问该计算机,以及
确认WinRM服务的防火墙例外已启用并允许从此计算机进行访问。默认情况下,公共配置文件的WinRM
防火墙例外会限制对同一本地子网内的远程计算机的访问。有关
的更多信息,请参见about_Remote_Troubleshooting帮助主题。
在第1行上的字符:12
+ $ session = new-pssession ec2-00-00-00-000.compute-1.amazonaws.com-凭证$ c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:OpenError:(System.Manageme .... RemoteRunspace:RemoteRunspace)[New-PSSession],PSRemotin
gTransportException
+ FullyQualifiedErrorId:WinRMOperationTimeout,PSSessionOpenFailed


解决方案

此处找到解决方案。



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



完整步骤:



EC2实例

1 )以管理员身份打开 PowerShell

2)输入 enable-psremoting -force

3)打开具有高级安全性的Windows防火墙

4)入站规则->查找 Windows远程管理(Http-In)-是2,请为两个都做

5)右键单击-> 属性-> 高级->检查公共



本地

6)以管理员身份打开 PowerShell < br>
7)输入 enable-psremoting -force

8)输入以下内容:

  $ password =转换为安全字符串-asplaintext -force -string MY_PASSWORD 
$ credential =新对象-typename system.management.automation.pscredential-参数列表 MY_USERNAME ,$ password
$ session = new-pssession MY_EC2_PUBLIC_IP-凭据$ credential
enter-pssession $ session
写主机 Hello,World(来自$ env:COMPUTERNAME)


I have an Amazon EC2 instance.

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).

Some I tried are
Managing Windows EC2 Instances remotely with Powershell
Administering EC2 instance with Windows Powershell
Enabling- PSRemoting
How to Run PowerShell Commands on Remote Computers

My understanding is that I need to:

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

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

//Local PowerShell(Administrator)
set-item wsman:\localhost\Client\TrustedHosts -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  

But I get this error

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

解决方案

Solution found here.

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

Full Steps:

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

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天全站免登陆