gpt4 book ai didi

powershell - 从我的本地计算机在 Amazon EC2 实例上远程执行 powershell 脚本

转载 作者:行者123 更新时间:2023-12-03 00:03:01 25 4
gpt4 key购买 nike

我有一个 Amazon EC2 实例。

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

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

我尝试过的一些是
Managing Windows EC2 Instances remotely with Powershell
Administering EC2 instance with Windows Powershell
Enabling- PSRemoting
How to Run PowerShell Commands on Remote Computers

我的理解是我需要:

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

但我收到此错误

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

最佳答案

找到解决方案here

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

完整步骤:

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

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

$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)"

关于powershell - 从我的本地计算机在 Amazon EC2 实例上远程执行 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com