AWS CLI - SSM Start Session - Execute Commands on EC2 instance after starting Session Manager using PowerShell Script

0

Hello,

I am writing a PowerShell script that access the file on ec2 instance using session manager with AWS CLI, calculate checksum of it and retrieve the checksum result.

I can be able to start the session using the PowerShell script but I am not sure how can I run PowerShell commands on EC2 instance using script?

$instanceId = "i-xxxxxxxxx"
$profile = "xxxxxxxx"
$filePath = "\path\to\file"

aws ssm start-session --target $instanceId --profile $profile
Get-FileHash -Path $filePath -Algorithm SHA256

I want to run "Get-FileHash -Path $filePath -Algorithm SHA256" command on EC2 instance using the PowerShell script and retrieve the result in PowerShell.

Can you provide me suggestions on how to execute PowerShell commands after establishing a session with EC2 instance using Session Manager With AWS CLI?

Thanks and Regards, Kamal

1 Answer
0

Hello.

I thought it would be better to use SSM RunCommand instead of using Session Manager in this case.
https://docs.aws.amazon.com/systems-manager/latest/userguide/run-command.html

If PowerShell is the target, I think it can be executed with AWS CLI using "AWS-RunPowerShellScript" as shown below.

aws ssm send-command --document-name "AWS-RunPowerShellScript" --document-version "1" --targets "Key=instanceids,Values=$instanceId" --parameters '{"commands":["Get-FileHash -Path $filePath -Algorithm SHA256"]}' 
profile picture
EXPERT
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions