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

Kamal
已提問 3 個月前檢視次數 214 次
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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南