0

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?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Rohit Singh
  • 47
  • 2
  • 10

1 Answers1

0

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.

Ketanbhut
  • 476
  • 2
  • 11
  • I am trying to run a script from s3: – Rohit Singh Jun 29 '20 at 17:29
  • below is the code : PS C:\Users\rsingh.ppcCLOUD> aws ssm send-command --document-name "AWS-RunRemoteScript" --targets "Key=instanceids,Values=i-bda3" --parameters "sourceType"="S3",sourceInfo='{\"path\":\"https://s3.amazonaws.com/xx/xxx/XXXXXX.ps1\"}',"commandLine"="XXXXXX.ps1 argument-1 argument-2" – Rohit Singh Jun 29 '20 at 17:37
  • but getting this error message: usage: aws [options] [ ...] [parameters] To see help text, you can run: aws help aws help aws help Unknown options: =XXXXXX.ps1 argument-1 argument-2, =S3,sourceInfo={"path":"https://s3.amazonaws.com/xx/xxx/XXXXXX.ps1\"},commandLine – Rohit Singh Jun 29 '20 at 17:37
  • Maybe a bit late to the party but...this worked ` aws ssm send-command --instance-ids "i-XXXXXXX" --document-name "AWS-RunRemoteScript" --parameters '{"sourceType":["S3"],"sourceInfo":["{\"path\":\"https://s3.us-east-1.amazonaws.com/cool-s3-bucket/update.sh\",\"commandLine\":\"update.sh\"}"]}' ` – David Velarde Oct 29 '22 at 19:45