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

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?

  • 1

    You are setting up a HTTP connection. To get results from ec2 you need to perform a HTTP query. See : docs.aws.amazon.com/AWSEC2/latest/APIReference/… In Powershell you use Invoke-WebRequest.

    – 




  • @jdweng I need to use AWS CLI commands not the API references. So, please suggest me in making use of AWS CLI commands to connect to EC2 instance, access the file and retrieve the checksum result

    – 

  • The command line for a query will look like this with a URL: : $ aws sqs receive-message –queue-url queue.amazonaws.com/546419318123/Test Also see aws.amazon.com/cli You use a URL where the parameter are after the question mark and have a ampersand between the parameters. Look at my link for the APIReference. You simply in the cli use the examples from the APIReference in the command line.

    – 




Leave a Comment