[英]In Azure DevOps pipelines, using variables in the powershell task results in ArgumentParseError when doing az login
我的管道中有一個 Powershell 任務:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
az login --service-principal --username $env:servicePrincipalId --password $env:servicePrincipalKey --tenant $env:tenantId
python $(Build.SourcesDirectory)/the/path/to/my/python/script.py
displayName: 'Execute Python code'
通過在此 Powershell 任務之前的 AzureCLI 任務中addSpnToEnvironment: true
向我提供服務主體詳細信息。
當 Powershell 任務運行時,我得到ArgumentParseError: argument --username/-u: expected one argument
。 我該如何解決這個問題?
我建議您使用Azure PowerShell 任務:
使用此任務在 Azure 環境中運行 PowerShell 腳本。 Azure 上下文通過提供的 Azure 資源管理器服務連接進行身份驗證。
# Azure PowerShell
# Run a PowerShell script within an Azure environment
- task: AzurePowerShell@4
inputs:
#azureSubscription: Required. Name of Azure Resource Manager service connection
#scriptType: 'FilePath' # Optional. Options: filePath, inlineScript
#scriptPath: # Optional
#inline: '# You can write your Azure PowerShell scripts inline here. # You can also pass predefined and custom variables to this script using arguments' # Optional
#scriptArguments: # Optional
#errorActionPreference: 'stop' # Optional. Options: stop, continue, silentlyContinue
#failOnStandardError: false # Optional
#azurePowerShellVersion: 'OtherVersion' # Required. Options: latestVersion, otherVersion
#preferredAzurePowerShellVersion: # Required when azurePowerShellVersion == OtherVersion
檢查密碼值是否以字符“ -
”開頭。
這是一個由前導字符“ -
”引起的已知問題,它使參數解析器將其混淆為選項名稱。 見這里。
作為一種解決方法,您可以通過在選項名稱和值之間添加“ =
”來解決此問題。
az login --service-principal --username=$env:servicePrincipalId --password=$env:servicePrincipalKey --tenant=$env:tenantId
此外,您還可以嘗試以下方法:
這個問題應該在最新版本的Azure CLI中已經修復了,您可以嘗試在代理機上升級Azure CLI到最新版本。
按照@Krzysztof Madej的建議嘗試使用Azure PowerShell 任務,並通過Azure 資源管理器服務連接設置授權。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.