【问题标题】:Unable to change active subscription in yaml/Azure PowerShell pipeline无法更改 yaml/Azure PowerShell 管道中的活动订阅
【发布时间】:2022-11-04 17:59:12
【问题描述】:

这是我在here 中遇到的问题的延续

让我介绍一下背景。这是我的 yaml 管道:

parameters:
- name: sub_name # name of the subscription; required
  type: string 
  default: false

steps:
  - script: echo "Here is subscription name:" ${{ parameters.sub_name }}
  - task: AzurePowerShell@5
    displayName: 'Launching Main.yml'
    inputs:
      azurePowerShellVersion: LatestVersion
      azureSubscription: My-SPN # This is my almighty Service Principal
      ScriptType: 'FilePath'
      ScriptPath: '$(System.DefaultWorkingDirectory)/MyPowerShell.ps1'
      ScriptArguments: -sub_name ${{ parameters.sub_name  }}

这是我的 MyPowerShell.ps1 文件:

#param ($sub_name)
Get-AzContext -ListAvailable | Where{$_.Name -match $sub_name} | Set-AzContext
$SID=(Get-AzContext).Subscription.id
Write-Output "The active subscription SID is" $SID

无论 $sub_name 给出什么值,$SID 的输出始终是我的服务主体的订阅 ID - “My-SPN”

我应该如何正确设置 AzContext 以便它更改活动订阅?

相同的 PowerShell 脚本在 Azure CLI 中运行良好,但在 yaml 获得服务主体时无法正常运行。 我尝试使用 Set-AzContext -Subscription $sub_name -TenantId 2a1c169e-715a-412b-b526-05da3f8412fa 但最终出现以下错误:

开始:启动 Main.yml ==================================================== ==============================任务:Azure PowerShell 描述:在 Azure 环境中运行 PowerShell 脚本 版本:5.209.0 作者:微软公司 求助:https://aka.ms/azurepowershelltroubleshooting ==================================================== ==============================生成脚本。 =========================== 启动命令输出 ===================== ====== "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "。'D:\a_temp\adfb7562-7db5-4be6-ae08-dca4664e460c.ps1'" 在会话中添加了 TLS 1.2。 导入模块-名称 C:\Modules\az_7.5.0\Az.Accounts\2.9.1\Az.Accounts.psd1 -Global警告:在这台机器上检测到 Az 和 AzureRM 模块。 Az 和 AzureRM 模块无法导入 相同的会话或在相同的脚本或运行手册中使用。如果您在您控制的环境中运行 PowerShell,您可以 使用“Uninstall-AzureRm”cmdlet 从您的计算机中删除所有 AzureRm 模块。如果您在 Azure 中运行 自动化,请注意您的任何 Runbook 都不会同时导入 Az 和 AzureRM 模块。可以找到更多信息 在这里:https://aka.ms/azps-migration-guide Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue Clear-AzContext -Scope 进程 Connect-AzAccount -ServicePrincipal -Tenant 2a1c169e-715a-412b-b526-05da3f8412fa -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope 设置-AzContext -SubscriptionId 72245732-XXXXXXX -TenantId 2a1c169e-XXXXXXXX##[错误]请提供有效的租户或有效的订阅。 ##[错误]PowerShell 以代码“1”退出。在会话中添加了 TLS 1.2。 完成:启动 Main.yml

请帮助了解如何在 yaml 或 powershell 文件中更改活动订阅。 谢谢。

【问题讨论】:

  • 您的服务主体是否具有其他订阅的权限?如果不是,则无法使用服务主体身份验证列出其他订阅。您提到“相同的 PowerShell 脚本在 Azure CLI 中运行良好”,您是在 Azure CLI 中本地运行它还是在管道内使用 Azure CLI 任务?
  • @ZiyangLiu-MSFT 你是对的,我使用的服务主体没有足够的订阅权限。修复它解决了这个问题。非常感谢你的帮助!

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


【解决方案1】:

就像上面提到的@ZiyangLiu-MSFT 一样,我使用的服务主体没有足够的订阅权限。将其更改为具有适当权限的那个,这解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2020-08-27
    • 2022-11-14
    相关资源
    最近更新 更多