如何解决Azure CLI 运行命令调用 RunPowerShellScript - 传递给脚本文件的参数被忽略
我正在尝试从 AzDO YAML 构建管道调用“az vm run-command invoke --command-id RunPowerShellScript ...”,但无法获取参数以成功传递到 Powershell 脚本文件。
还有其他与此类似的 SO 问题,尤其是这似乎具有我需要的答案的问题,但它对我不起作用:Azure CLI run-command invoke RunPowerShellScript with parameters。 我还可以从 this answer 看到参数不能是布尔类型,由此我假设除“字符串”之外的任何类型也是不允许的,但是我在 ps1 文件输入参数上添加了字符串类型属性这并不能解决我的问题。
实际行为
脚本本身确实执行,但无论我尝试以何种方式传递参数,它们始终被忽略。我可以看到记录到标准输出的值是脚本文件中参数设置的默认值:$matchServiceNamePattern
设置为 'defaultservicename' 和 $maxnumberOfTries
设置为“10” - 即不是传入的值.不为参数指定默认值没有任何区别,但对我的用例来说不是很安全。
预期行为
我希望我通过 YAML 文件中 Azure CLI 命令中的“--parameters matchServiceNamePattern=servicename maxnumberOfTries=25
”开关传入的参数传递给脚本。
设置
- 代理操作系统:Windows
- 目标虚拟机操作系统:Windows
- 脚本语言:Powershell
- 脚本位置:作为 VM 上的本地文件引用,而非内联
我的代码的相关部分是(下面的 YAML 显示了以我认为应该根据其他 SO 答案工作的方式传递的参数):
AzureCLI YAML 管道任务:
- task: AzureCLI@2
inputs:
azureSubscription: $(AZURE_SUBSCRIPTION_NAME)
scriptType: ps
scriptLocation: inlinescript
inlinescript: |
az vm run-command invoke --command-id RunPowerShellScript -g $(AZURE_RESOURCE_GROUP_NAME) -n $(AZURE_VM_NAME) --scripts C:\check_service_running.ps1 --parameters matchServiceNamePattern=servicename maxnumberOfTries=25
check_service_running.ps1 Powershell 脚本文件
param (
[string]
$matchServiceNamePattern = 'defaultservicename',[string]
$maxnumberOfTries = "10")
$matchServiceNamePatternWildcard = $matchServiceNamePattern + '*'
$maxnumberOfTriesAsInt = [int]$maxnumberOfTries
$maxRepeat = $maxnumberOfTriesAsInt
$currentIteration = 1
do
{
$matchingServices = Get-Service $matchServiceNamePatternWildcard
$matchingServicesCount = ($matchingServices).count
$runningServiceCount = ($matchingServices | ? {$_.status -eq "Running"}).count
$difference = $matchingServicesCount - $runningServiceCount
Write-Host "Checking to see if Service(s) matching '$matchServiceNamePatternWildcard' are all in a Running state. `
Total service count: $matchingServicesCount,Running service count: $runningServiceCount. `
Iteration $currentIteration of $maxnumberOfTries"
$maxRepeat--
$currentIteration++
Start-Sleep -Milliseconds 6000
} until (($difference -le 0) -or $maxRepeat -eq 0)
Write-Host "Service running check complete."
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。