How to programmatically/remotely execute a program in EC2 Windows instance How to programmatically/remotely execute a program in EC2 Windows instance windows windows

How to programmatically/remotely execute a program in EC2 Windows instance


An alternative approach is to use Windows powershell and WinRM - it allows for remote execution, a bit like ssh on Linux.

Here is a sample of a powershell script you can run on the client to remote execute a script (taken from: https://github.com/CloudifySource/cloudify/blob/master/esc/src/main/resources/clouds/ec2-win/upload/bootstrap-client.ps1):

param ([string]$target, [string]$username, [string]$password, [string]$command)$ErrorActionPreference="Stop"# Set up the password$securePassword = ConvertTo-SecureString -AsPlainText -Force $password$cred = New-Object System.Management.Automation.PSCredential $username, $securePasswordWrite-Host "Connecting to management service of $target"Connect-WSMan -Credential $cred $target set-item WSMan:\$target\Client\TrustedHosts -Value * -Forceset-item WSMan:\$target\Shell\MaxMemoryPerShellMB -Value 0 -ForceWrite-Host Invoking command on Remote host $targetInvoke-Command -ComputerName $target -Credential $cred  -ScriptBlock {      Invoke-Expression $args[0]} -ArgumentList $commandWrite-Host "Command finished"

You can run this command from your own script with the following command:

powershell.exe -inputformat none -File PATH_TO_SCRIPT -target TARGET_IP -password PASSWORD -username USERNAME -command COMMAND_TO_EXECUTE

You should probably quote your strings, especially the password and command, as these will usually have special characters that powershell can interpret as something else.

The WinRM service is on by default on the EC2 Amazon Windows AMIs. All you need to do is open port 5985 (the WinRM port) in your security group.

Finally, if you have never used powershell remoting on your client machine before, there are a couple of commands you should execute to set it up (you only need to do this once):

set-item WSMan:\localhost\Client\TrustedHosts -Value * -Forceset-item WSMan:\localhost\Shell\MaxMemoryPerShellMB -Value 0 -ForceEnable-PSRemotingSet-ExecutionPolicy unrestricted

Make sure to run these as an Administrator.


The command ec2-run-instances has two additional arguments that can be used when running the command. The user-data command and user-data-file both of these perform the same task just they read from different input. When you use this argument the contents of the user-data will be uploaded to a amazon hosted URI http://169.254.169.254/1.0/user-data only available to the instance that was launched.

The normal way to do this in the linux environment would be to upload a shell script to the instance to download the exe, your user-data-file might look something like this...

#! /bin/bashwget http://www.domain.com/my-file.exe

In Windows there's no default service installed to execute the user-data-file when the instance is booted but there is an open-source project CloudInit.NET which simulates the same process but with a powershell script. The only requirements are .NET 4.0 and CloudInit.NET. Once installed it will execute the user-data-file when the instance is booted. It's very easy to download a file and execute it with a powershell script.

!# /powershell/$wc = New-Object System.Net.WebClient$wc.DownloadFile("http://www.domain.com/my-file.exe", "C:\my-file.exe");& 'C:\my-file.exe'


You can handle this in 2 ways,

  • Using winscp in Windows SFTP program.

    To access your Amazon server using SFTP on Windows, download a Windows SFTP application. Using WinSCP you’ll establish an SFTP session with your server. WinSCP offers some nice features that make it easy to work with your EC2 server. For example, a command in the button bar opens a PuTTY SSH terminal session using the same credentials you used for your SFTP session. (You can also launch a PuTTY session by clicking CTRL+P.).

  • Get an S3 bucket and mount on all your windows and linux EC2 instances. You should be able to upload and download the files to S3 bucket from your workstation, which are accessible to your instances.