在 Azure DevOps 管道中,在 powershell 任务中使用变量会导致在执行 az login 时出现 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 task 其中:
Use this task to run a PowerShell script within an Azure environment. The Azure context is authenticated with the provided Azure Resource Manager service connection.
# 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
检查密码值是否以字符“-
”开头。
这是一个已知问题,由前导字符“-
”导致参数解析器将其混淆为选项名称。参见 here。
作为解决方法,您可以通过在选项名称和值之间添加“=
”来解决此问题。
az login --service-principal --username=$env:servicePrincipalId --password=$env:servicePrincipalKey --tenant=$env:tenantId
此外,您还可以尝试以下方法:
这个问题应该已经在最新版本的Azure CLI中得到修复,您可以尝试在代理机器上将Azure CLI升级到最新版本。
尝试按照 @Krzysztof Madej 的建议,使用 Azure PowerShell task, and setting the authorization via an Azure Resource Manager service connection.
我的管道中有一个 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 task 其中:
Use this task to run a PowerShell script within an Azure environment. The Azure context is authenticated with the provided Azure Resource Manager service connection.
# 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
检查密码值是否以字符“-
”开头。
这是一个已知问题,由前导字符“-
”导致参数解析器将其混淆为选项名称。参见 here。
作为解决方法,您可以通过在选项名称和值之间添加“=
”来解决此问题。
az login --service-principal --username=$env:servicePrincipalId --password=$env:servicePrincipalKey --tenant=$env:tenantId
此外,您还可以尝试以下方法:
这个问题应该已经在最新版本的Azure CLI中得到修复,您可以尝试在代理机器上将Azure CLI升级到最新版本。
尝试按照 @Krzysztof Madej 的建议,使用 Azure PowerShell task, and setting the authorization via an Azure Resource Manager service connection.