1- #! /usr/bin/env sh 
1+ # !/usr/bin/env pwsh 
22# ####### Source ################################################################
33# 
44#  https://github.com/qoomon/aws-ssm-ec2-proxy-command
1313#  Add following SSH Config Entry to ~/.ssh/config
1414#  host i-* mi-*
1515#  IdentityFile ~/.ssh/id_rsa
16- #  ProxyCommand powershell  .exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub
16+ #  ProxyCommand powershell.exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub
1717#  StrictHostKeyChecking no
1818# 
1919#  Ensure SSM Permissions for Target Instance Profile
2020#  https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-instance-profile.html
2121# 
2222#  Open SSH Connection
2323#  ssh <INSTANCE_USER>@<INSTANCE_ID>
24- #   
24+ # 
2525#  Ensure AWS CLI environment variables are set properly
2626#  e.g. AWS_PROFILE='default' ssh ec2-user@i-xxxxxxxxxxxxxxxx
2727# 
@@ -49,31 +49,30 @@ if ($splitted_instance.Length -gt 1)
4949 $env: AWS_DEFAULT_REGION  =  $splitted_instance [1 ]
5050}
5151
52- $authorized_key  =  " $ssh_public_key  ssm-session " 
53- $script   = @" 
54- \" 
55- mkdir -p ~$ssh_user /.ssh && cd ~$ssh_user /.ssh || exit 1 
52+ Write-Output   " Add public key  $ssh_public_key_path  for  $ssh_user  at instance  $ec2_instance_id  for  $ssh_public_key_timeout  seconds " 
53+ $addPublicKeyScript   =  @" 
54+   \"
55+   mkdir -p ~$ssh_user /.ssh && cd ~$ssh_user /.ssh || exit 1
5656
57- echo '$authorized_key ' >> authorized_keys 
57+  authorized_key='$ssh_public_key  ssm-session' 
58+  echo \\\"`$ authorized_key\\\" >> authorized_keys 
5859
59- sleep $ssh_public_key_timeout  
60+   sleep $ssh_public_key_timeout 
6061
61- grep -v -F ' $authorized_key '  authorized_keys > .authorized_keys 
62- mv .authorized_keys authorized_keys 
63- \" 
62+   grep -v -F \\\" ` $ authorized_key\\\"  authorized_keys > .authorized_keys
63+   mv .authorized_keys authorized_keys
64+   \"
6465"@ 
65- 
66- Write-Output " Add public key $ssh_public_key_path  for $ssh_user  at instance $ec2_instance_id  for $ssh_public_key_timeout  seconds" 
6766aws ssm send-command  ` 
6867 -- instance- ids " $ec2_instance_id "   ` 
6968 -- document- name ' AWS-RunShellScript'   ` 
7069 -- comment " Add an SSH public key to authorized_keys for $ssh_public_key_timeout  seconds"   ` 
71-  --parameters commands=" $script  " 
70+  -- parameters commands= " $addPublicKeyScript  " 
7271if ($LASTEXITCODE  -ne  0 ) { Write-Error  " Failed to add public key with error $output "   }
7372
7473Write-Output  " Start ssm session to instance $ec2_instance_id " 
7574aws ssm start-session  ` 
7675 -- target " $ec2_instance_id "   ` 
7776 -- document- name ' AWS-StartSSHSession'   ` 
7877 -- parameters " portNumber=$ssh_port " 
79- if($LASTEXITCODE  -ne 0) { Write-Error " Failed to start ssm session to instance $output "   } 
78+ if ($LASTEXITCODE  -ne  0 ) { Write-Error  " Failed to start ssm session to instance $output "   }
0 commit comments