Azure Devops - Azure 资源管理器 (ARM) 服务连接

标签 azure powershell azure-devops

我有一个 PowerShell 脚本,我想将其移至 Azure DevOps 管道中。

PowerShell 脚本执行 Connect-AzAccount,然后迭代我的帐户可以看到的所有约 300 个订阅:

foreach ( $Subscription in $(Get-AzSubscription | Where-Object { $_.State -ne "Disabled" }) ) {...}

DevOps 管道使用 AzurePowerShell@5 任务中的服务连接:

- task:  AzurePowerShell@5
  inputs:
    azurePowerShellVersion: 'LatestVersion'
    ConnectedServiceNameARM: test-infosec-connection
    scriptPath: './_AzureWafCompliance.ps1'
    pwsh: true

作业成功运行并完成,但问题是它仅迭代一个订阅,即 ARM 服务连接订阅。

在调试中可以看到 AzurePowerShell 任务运行以下命令:

Connect-AzAccount -ServicePrincipal -Tenant xxxxxxx-xxxxxxx-xxxxxx-xxxxxx-xxxxxx -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope

 Set-AzContext -SubscriptionId xxxxxx-xxxx-xxxx-xxxx-xxxxxx -TenantId xxxx-xxxx-xxxx-xxxx-xxxxxx

我怀疑这是第二个命令将我锁定在订阅中。是否有另一种/更好的方法,或者有任何方法可以更改 AzurePowershell 任务的默认行为?

最佳答案

Azure PowerShell 任务用于在 Azure 环境中运行 PowerShell 脚本。 Azure 上下文通过提供的 Azure 资源管理器服务连接进行身份验证。

在本地,该脚本会使用您的凭据运行,并且会成功,因为您可能拥有所需的访问权限。

创建 ARM 服务连接订阅时,必须选择 Azure 订阅。

enter image description here

因此,您的 Azure PowerShell 任务仅迭代此选定的 Azure 订阅。

使用 ARM 服务连接管理组而不是 ARM 服务连接订阅:https://learn.microsoft.com/en-us/azure/governance/management-groups/overview

enter image description here

关于Azure Devops - Azure 资源管理器 (ARM) 服务连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73225539/

相关文章:

azure - 启用防火墙时将 ADLS Gen2 安装到 Databricks

Azure 静态网站到 CDN 终结点映射未显示

arrays - 在 PowerShell 中将哈希表数组中的元素移动到另一个数组

azure-devops - 使用 oAuth 访问 Azure DevOps REST API

azure-devops - 如何通过 Rest API 获取 AzureDevOps 构建变量信息?

c# - 开发环境DistributedCache.RoutingClient错误

asp.net - Azure WebApp 的起点

asp.net - 卡在ASP.NET 5/VS2015中的DNX引用问题上

powershell - 从CSV将字符串转换为Powershell中的DateTime

azure - 如何在azure pipeline yaml文件的maven目标中添加标签