Skip to content

Commit 615fca1

Browse files
committed
ssm-send-command accepts a script (linux only)
1 parent dc4f1fe commit 615fca1

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lib/ssm-functions

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,49 @@ ssm-send-command() {
6666
#
6767
# See also: ssm-send-command-windows
6868

69-
local bma_document_name="${bma_document_name:-AWS-RunShellScript}"
70-
7169
local command=$1
7270
shift
7371
local instances=$(skim-stdin "$@")
74-
7572
if [[ -z $command ]] || [[ -z $instances ]]; then
7673
echo "Usage: $0 command instance-id [instance-id]" >&2
7774
return 1
7875
fi
7976

77+
local ssm_target_platform="${ssm_target_platform:-Linux}"
78+
local document_name
79+
80+
if [[ $ssm_target_platform = Linux ]]; then
81+
document_name='AWS-RunShellScript'
82+
elif [[ $ssm_target_platform = Windows ]]; then
83+
document_name='AWS-RunPowerShellScript'
84+
else
85+
echo >&2 "Invalid value (\$ssm_target_platform ${ssm_target_platform})"
86+
return 1
87+
fi
88+
89+
if [[ $ssm_target_platform = Linux ]]; then
90+
91+
# If the command is a file
92+
if [[ -f $command ]]; then
93+
# Load contents of the file into variable
94+
command=$(<"$command")
95+
fi
96+
97+
# Base64 encode the command if it's a script
98+
if [[ $command == *$'\n'* ]]; then
99+
command=$(echo "$command" | base64)
100+
command="echo $command | base64 --decode | bash"
101+
fi
102+
fi
103+
80104
# Escape double quotes in command
81105
local escaped_command=$(echo $command | sed 's/"/\\"/g')
82106

83107
# Send command
84108
local command_id
85109
if ! command_id=$(aws ssm send-command \
86110
--targets "Key=instanceids,Values=${instances// /,}" \
87-
--document-name "${bma_document_name}" \
111+
--document-name "${document_name}" \
88112
--parameters "{\"commands\":[\"$escaped_command\"]}" \
89113
--query "Command.CommandId" \
90114
--output text); then
@@ -147,7 +171,7 @@ ssm-send-command-windows() {
147171
# FAKEAPP01234 Security Update KB5678901 NT AUTHORITY\SYSTEM 12/12/2018 12:00:00 AM
148172
#
149173
# See also: ssm-send-command-windows
150-
local bma_document_name='AWS-RunPowerShellScript'
174+
local ssm_target_platform='Windows'
151175
ssm-send-command "$@"
152176
}
153177

0 commit comments

Comments
 (0)