azure - 在 Azure DevOps 管道中,在 powershell 任务中使用变量会导致在执行 az login 时出现 ArgumentParseError

标签 azure powershell azure-devops azure-pipelines azure-service-principal

我的管道中有一个这样的 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:参数 --username/-u:需要一个参数。我该如何解决这个问题?

最佳答案

我建议您使用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

关于azure - 在 Azure DevOps 管道中,在 powershell 任务中使用变量会导致在执行 az login 时出现 ArgumentParseError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65145148/

相关文章:

azure - Terraform keyvault 删除保护无法按预期工作

git - 如何通过终端创建 pull 请求?

git - 带有私有(private)子模块的Azure github部署

Python azure 模块: how to create a new deployment

azure - 无法从 Kubernetes 获取 apiVersions : Unable to retrieve the complete list of server APIs

string - Terraform 将元组展平为字符串集

c# - 如何检查 cobertura.xml 文件的代码/测试覆盖率是否高于指定阈值?

exception - 如何将 powershell 异常描述转换为字符串?

tfs - 将工作项从本地 TFS 迁移到 VSTS

Azure Devops : what does changed date mean? 它跟踪什么样的更改?