【问题标题】:Azure DevOps - Cannot recognize "Get-AzSubscription" in powershellAzure DevOps - 在 powershell 中无法识别“Get-AzSubscription”
【发布时间】:2022-01-06 22:48:45
【问题描述】:

我在 Azure DevOps 中定义了以下管道,以在 linux 代理中运行 Az 模块 powershell。

- main

#pool: Default

pool:
  vmImage: 'ubuntu-latest'

steps:

- task: AzureCLI@2
  inputs:
    azureSubscription: sp-test
    scriptType: pscore
    scriptLocation: 'inlineScript'
    inlineScript: |
      $Subscriptions = Get-AzSubscription
  displayName: 'Run Powershell'

它给了我一个错误

Get-AzSubscription : The term 'Get-AzSubscription' is not recognized as a name of a cmdlet, function, script file, or executable program.

同样的方法适用于运行 az 命令的 bash。我需要先在 linux 代理上安装任何 Powershell 模块吗?

【问题讨论】:

    标签: azure-devops azure-pipelines azure-powershell


    【解决方案1】:

    如果要运行 azure powershell 命令Get-AzSubscription,只需使用Azure PowerShell Task,Linux 代理也支持。

    如果你还想使用 Azure CLI Task,你可以直接使用az account show 命令,它和powershell 命令Get-AzSubscription 做同样的事情。

    原因是不同的任务会自动为你登录到 azure,例如对于 azure powershell 任务,它将使用Connect-AzAccount 登录;对于 azure cli 任务,它将使用az login 登录。不建议将它们混合在一起。

    【讨论】:

      【解决方案2】:

      Get-AzSubscription 是一个 PowerShell 命令,您使用 AzureCLI@2 任务,它用于 az 命令。

      你只需要使用AzurePowerShell@5任务:

      - task: AzurePowerShell@5
        displayName: 'Azure PowerShell script: InlineScript'
        inputs:
          azureSubscription: 'sp-test'
          ScriptType: InlineScript
          Inline: '$Subscriptions = Get-AzSubscription'
          azurePowerShellVersion: LatestVersion
      

      【讨论】:

      • 谢谢,但这里有两件事。 AzurePowerShell@5 可以在 Linux 代理上运行吗?对于我的 AzureCli@2 任务,我将其标记为 pscore 脚本类型。不能用来运行Powershell命令吗?
      • 它当然应该能够运行powershell命令。但也许它不允许您从 azure powershell 模块运行命令?
      • 没错。我可以按照我展示的方式运行普通的 powershell,但不能运行 Az powershell。不知道为什么
      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2020-08-27
      相关资源
      最近更新 更多