Python变量和括号

时间:2019-03-10 15:02:49

标签: python

我在Lambda中有一些代码,想使用Lambda Environment变量保护密码。

现在的代码如下:

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 \
"]} )`

考虑到所有这些括号,如何将superPassword123替换为os.environ['DomainPassword']

1 个答案:

答案 0 :(得分:1)

您可以使用字符串格式传递密码(或python中的其他任何硬编码子字符串:

"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'])