【发布时间】: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