Send multiple lines of script to EC2 instance from PowerShell SSM cmdlets

6k views Asked by At

I have this example from AWS sites:

Send-SSMCommand -DocumentName "AWS-RunPowerShellScript" -Parameter @{commands = "echo helloWorld"} -Target @{Key="instanceids";Values=@("i-0cb2b964d3e14fd9f")}

in which one line of PowerShell script (echo helloworld) is being sent.

What if I have to send multiple line of PowerShell script through SSM. How to do that?

1

There are 1 answers

4
Ketanbhut On

There are multiple ways you can achieve this. You can provide a script to run, which resides inside the instance. or you can create an array of commands - like this:

$commands = @("ipconfig","hostname","write-output 'nothing new here'","get-service")
Send-SSMCommand -InstanceId $instance -Parameter @{commands = $commands} -DocumentName "AWS-RunPowerShellScript"

You can simply run multiple commands with ';' separator - like this:

Send-SSMCommand -InstanceId $instance -Parameter @{commands = "ipconfig;hostname"} -DocumentName "AWS-RunPowerShellScript"

Here is an example - you could run the script directly - which can have multiple lines:

Send-SSMCommand -InstanceId $instance -Parameter @{commands = "c:\programdata\get-param.ps1"} -DocumentName "AWS-RunPowerShellScript"

You could also run the remote script with a custom document, which is either in s3 or github - find examples here: https://docs.aws.amazon.com/systems-manager/latest/userguide/integration-remote-scripts.html

I hope this helps.