Managing Windows EC2 Instances remotely with Powershell

For a long time now I'm trying to find out how to remotely manage an EC2 machine with Powershell.It finally worked. Here is how:

  1. Start a new Windows Server 2008 EC2 instance. Keep the machine name, public DNS name and password for your EC2 machine handy. You'll need it later.
  2. Configure your Security Groups (which are firewall rules in essence) to allow connections to port 5985.
  3. Connect to it and install  PowerShell 2.0 for Windows Server 2008. Reboot.
  4. After the reboot I pretty much followed this walkthrough. Here the short version:
    a) Run Enable-PSRemoting –force on your EC2 machine; elevated of course.
    b) On the client, assuming it's Win7 runthe following Powershell commands:
       Start-Service WinRM
      
    Set-ItemProperty –Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System –Name  LocalAccountTokenFilterPolicy –Value 1 –Type DWord
      
    Set-Item WSMan:\localhost\Client\TrustedHosts –Value <Public DNS Name of your EC2 machine> -Force -Concatenate
  5. You are ready to go. Connect from your client to EC2 with the New-PSSession cmdlet, for example:
    New-PSSession -credential <machine name of your EC2 machine>\Administrator -computername <Public DNS Name of your EC2 machine>
  6. To execute commands on your EC2 machine I used the Enter-PSSession cmdlet, for example:
    Enter-PSSession <id returned by New-PSSession>
  7. After this you'll have a command-line on your EC2 machine.

No Comments