【发布时间】:2021-05-10 19:12:38
【问题描述】:
我有一个 AzureDevops Powershell 任务,它将从 KeyVault 读取的值作为参数传递给脚本。即使在 DevOps 任务中使用双引号或单引号将变量括起来,脚本中的值也不正确。
我还需要做什么才能将这些值正确传递给脚本?
有一个older similar question,答案是双引号任务中的参数,我已经这样做了,但无济于事。
Powershell 任务示例:
ScriptArguments: -connectionString "$(ConnString)" -password "$(ConnStringPassword)"
存储在 KeyVault 中的示例值:
ConnString: Server=tcp:test-sqldb.database.windows.net,1433;Initial Catalog=TestDb;Persist Security Info=False;
ConnStringPassword: Pa$$w0rd
示例脚本:
param (
[string]$connectionString,
[string]$password
)
Write-Host "connectionString: $connectionString"
Write-Host "password: $password"
管道执行的示例输出:
connectionString: ***
password: Paw0rd
如果我按照下面@Thomas 的建议在管道任务中使用单引号:
Powershell 任务示例:
ScriptArguments: -connectionString '$(ConnString)' -password '$(ConnStringPassword)'
流水线执行的示例输出:
connectionString: ***
password: ***
【问题讨论】:
-
双引号允许 PS 评估内容,从而将
$符号解释为变量名的开头。改用单引号。 -
谢谢@Thomas,也试过了。在上面发布了进一步的结果。类似的问题。
-
ScriptArguments: ' -connectionString "$(ConnString)" -password "$(ConnStringPassword)"'怎么样? (在整个事情周围添加单引号) -
来自 KeyVault 的值是 secrets,您看不到这些值。
-
@ShaykiAbramczyk 更新:你是 100% 正确的。我通过在测试输出文件上执行发布步骤验证了 KV 值在 Powershell 脚本中得到了正确利用。请参阅我发布的答案和其中的链接以供进一步参考。感谢参与。
标签: powershell azure-devops azure-powershell