kagarlickij
kagarlickij

Reputation: 8137

Python variable and brackets

I have some code in Lambda and want to secure password with Lambda Environment variables.

Now code look like this:

command = ssm_client.send_command( \
InstanceIds=[InstanceId], \
DocumentName='AWS-RunPowerShellScript', \
Parameters={ \
  "commands":[" \
    $pass = ConvertTo-SecureString superPassword123 -AsPlainText -Force; \
    $creds = New-Object System.Management.Automation.PSCredential -ArgumentList kagarlickij\Admin,$pass; \
    Remove-Computer -ComputerName $(hostname) -Credential $creds -Verbose -Restart -Force \
"]} )`

How can I replace superPassword123 with os.environ['DomainPassword'] taking into account all those brackets?

Upvotes: 0

Views: 389

Answers (1)

combinatorist
combinatorist

Reputation: 565

You can pass the password (or any other hardcoded substring in python with string formatting:

"commands":[" \
    $pass = ConvertTo-SecureString {pw} -AsPlainText -Force; \
    $creds = New-Object System.Management.Automation.PSCredential -ArgumentList kagarlickij\Admin,$pass; \
    Remove-Computer -ComputerName $(hostname) -Credential $creds -Verbose -Restart -Force \
".format(pw = os.environ['DomainPassword'])

Upvotes: 1

Related Questions