I'm trying to build a bash for loop that feeds instance IDs into the following AWS CLI command to run Powershell on multiple instances. Whenever I try to expand the value of $server either as a variable for as a list of instance IDs in a for loop I get the error:
An error occurred (InvalidInstanceId) when calling the SendCommand operation:
Here are a couple of attempts to get $server to expand properly when executing the Run document from an AWS CLI enabled Linux box. When I replace $server or ${server} with the same instance ID that the variable $server contains (i-081158da57d2a8da6) it works without issue. I feel like this must be extremely simple but my research skills are failing me at figuring out how one would expand the variable within the syntax required to properly execute the SSM document from AWS CLI on my Linux instance.
:~$ aws ssm send-command --document-name "AWS-RunPowerShellScript" --document-version "1" --targets '[{"Key":"InstanceIds","Values":["${server}"]}]' --parameters '{"commands":["$wmi = Get-WmiObject -Class Win32_OperatingSystem ","$uptimeMinutes = ($wmi.ConvertToDateTime($wmi.LocalDateTime)-$wmi.ConvertToDateTime($wmi.LastBootUpTime) | select-object -expandproperty "TotalMinutes")","[int]$uptimeMinutes"],"workingDirectory":[""],"executionTimeout":["60"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-west-2
An error occurred (InvalidInstanceId) when calling the SendCommand operation:
:~$ aws ssm send-command --document-name "AWS-RunPowerShellScript" --document-version "1" --targets '[{"Key":"InstanceIds","Values":["$server"]}]' --parameters '{"commands":["$wmi = Get-WmiObject -Class Win32_OperatingSystem ","$uptimeMinutes = ($wmi.ConvertToDateTime($wmi.LocalDateTime)-$wmi.ConvertToDateTime($wmi.LastBootUpTime) | select-object -expandproperty "TotalMinutes")","[int]$uptimeMinutes"],"workingDirectory":[""],"executionTimeout":["60"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-west-2
An error occurred (InvalidInstanceId) when calling the SendCommand operation: