Powershell to EC2 instance – Installing executable files on EC2 instance

amazon ec2powershell

I am new to Amazon servers. I recently created an EC2 Windows2008 R2 instance and tried to connect to the instance from my system using powershell ALONE

I don't want to use Amazon API in connecting, transferring a file from local to EC2, or installing the files in the EC2 instance.

I want to accomplish all these just by using POWERSHELL.

I am able to connect and transfer files from local to EC2, Can some one suggest/ help to install the executable files on this EC2 instance.

Any help in this matter is deeply appreciated! thx in advance

Best Answer

If you can install your executable from the command line ('silent install') then powershell remoting is the best choice. It is on by default on the Amazon Windows images, so you don't need to do anything other then start up the machine and grab the password.

There are instructions for setting up remoting here: https://stackoverflow.com/a/13284313/1335661

The bottom line is that you can use the following script to execute a remote command (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, $securePassword

Write-Host "Connecting to management service of $target"
Connect-WSMan -Credential $cred $target 

set-item WSMan:\$target\Client\TrustedHosts -Value * -Force
set-item WSMan:\$target\Shell\MaxMemoryPerShellMB -Value 0 -Force

Write-Host Invoking command on Remote host $target
Invoke-Command -ComputerName $target -Credential $cred  -ScriptBlock {  
    Invoke-Expression $args[0]
} -ArgumentList $command
Write-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
Related Topic