【问题标题】:AzureDevOps Powershell task command line parameters are not properly rendered in Powershell scriptAzureDevOps Powershell 任务命令行参数未在 Powershell 脚本中正确呈现
【发布时间】: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


【解决方案1】:

@ShaykiAbramczyk 是正确的,秘密是不可见的。然而,这些值在 Powershell 脚本中得到了正确使用。我通过发布测试输出文件并查看 KeyVault 值在输出文件中来验证这一点。以下参考给了我发布以查看值的想法,而不是尝试将它们回显或写入控制台。

Use secrets from Azure Key Vault in Azure Pipelines

【讨论】:

  • 嗨@Mike,感谢分享,你可以Accept it as an Answer,它可以帮助遇到同样问题的其他社区成员,我们可以存档这个帖子,谢谢。祝你今天过得愉快。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 2018-12-22
相关资源
最近更新 更多