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 回答
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
专家
已回答 3 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则