简体   繁体   English

AWS ssm 中的变量分配不起作用

[英]Variable assignment inside AWS ssm does not work

I am trying to run below command on AWS SSM:我正在尝试在 AWS SSM 上运行以下命令:

Parameters = @{
           commands = @(
           "Write-Host userid is  $userID password $($password)"
           '$userID2 = $userID'
           '$password2 = $password'
           "Write-Host userid is  $userID2 password $($password2)"
       ) 
        }

First Write-Host statement prints the correct values of $userID and $password but after the assignment of the new variable, second Write-Host variable prints empty for both variables.第一个 Write-Host 语句打印 $userID 和 $password 的正确值,但在分配新变量后,第二个Write-Host变量对两个变量都打印为空。 Am I doing something wrong?难道我做错了什么? I tried fetching the values of $userID and $password with double quotes as well but no luck.我也尝试用双引号获取$userID$password的值,但没有运气。

For anyone facing the same issue.对于任何面临同样问题的人。 Problem is that before sending the commands to EC2, AWS will replace the commands with all variable values and hence, we cannot reuse the variables inside the commands directly.问题是在将命令发送到 EC2 之前,AWS 将用所有变量值替换命令,因此,我们不能直接重用命令中的变量。 This is the workaround I had to come up with:这是我必须想出的解决方法:

$userID = 'testuser1'
$password = 'testpassword'
$userIdString = "`$userID2 = '$userID'"
$passwordString = "`$password2 = '$password'"

Parameters = @{
           commands = @(
           "Write-Host userid is  $userID password $($password)"
           "$userIdString"
           "$passwordString"
           "Write-Host userid is  $userID2 password $($password2)"
       ) 
        }

Now it prints the $userID2 and $password2 fine.现在它打印$userID2$password2罚款。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM