【问题标题】:Connect-AzureAD using powershell in Azure Devops在 Azure Devops 中使用 powershell 连接 AzureAD
【发布时间】:2021-08-15 05:16:45
【问题描述】:

我正在使用此代码通过 ADO 管道自动连接到 AzureAD,这是从另一篇文章中得到的:

- task: AzurePowerShell@5
      inputs:
        azureSubscription: '<My service connection name>'
        ScriptType: 'InlineScript'
        Inline: |
          Install-Module -Name AzureAD -AllowClobber -Scope CurrentUser -Force
          $context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
          $graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.microsoft.com").AccessToken
          $aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken
          Write-Output "Hi I'm $($context.Account.Id)"
          Connect-AzureAD -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id -MsAccessToken $graphToken
          
        azurePowerShellVersion: 'LatestVersion'
      displayName: " Install Azure AD module"```

- task: AzurePowerShell@5
      inputs:
        azureSubscription: '<My service connection name>'
        ScriptType: 'InlineScript'
        Inline: |
        
          Get-AzureADMSApplication -Filter "DisplayName eq 'My application'" 
       azurePowerShellVersion: 'LatestVersion'
      displayName: "Get Application Name"

代码运行良好,但第二个任务是 Get an Application : 我在管道中收到错误消息“##[错误]您必须先调用 Connect-AzureAD cmdlet,然后才能调用任何其他 cmdlet。”

为避免这种情况,我需要在每个 azure powershell 任务中添加代码(在第一个任务中)以连接到 AzureAD。

有什么办法可以避免这种情况,一旦在第一个任务中使用 Connect-AzureAD 代码连接,我就不必在其余任务中添加此代码?

【问题讨论】:

    标签: azure azure-devops azure-pipelines


    【解决方案1】:

    下一个 azure powershell 任务将在新会话中执行,因此您必须再次使用 Connect-AzureAD 代码以确保可以启动后面的 cmdlet,外观应与您的本地计算机相同。

    如果您只想添加一次代码,您可以将其他 comlet 放在同一个 powershell 任务中作为替代方案,谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多