簡體   English   中英

在 Azure DevOps 管道中,在執行 az login 時使用 powershell 任務中的變量會導致 ArgumentParseError

[英]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

此外,您還可以嘗試以下方法:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM