Execute powershell script remotely on Amazon EC2 instance from my local computer Execute powershell script remotely on Amazon EC2 instance from my local computer powershell powershell

Execute powershell script remotely on Amazon EC2 instance from my local computer


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


I think that not exposing PowerShell via SSH was one of the biggest design mistakes MS did. Even years later they are too proud / blind to do revert that poor decision.

I suggest you to not fight with WinRM and instead, use an SSH server on your Windows machine.You'll benefit from having a simple, standard, secure way to connect to your server from any device (I'm doing remote PS sessions from my iPad).

There is the opensource cygwin and my favorite proprietary (with free offering) PowershellServer

You'll thank me when your Windows server will play nicely with the rest of the world.


UPDATEI got back to this old thread and would like to add another option - using the new(ish) AWS Systems Manager run-command capability.This allows you to have no administrative port exposed to the external world so no need to fiddle with host / cloud firewalls.It also provide other benefits like auditing, permissions etc...