在 Azure Devops 中使用 powershell 自动化 Connect-AzureAD [英] Automate Connect-AzureAD using powershell in Azure Devops

查看:32
本文介绍了在 Azure Devops 中使用 powershell 自动化 Connect-AzureAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法自动执行 Connect-AzureAD powershell 命令.

I am unable to automate Connect-AzureAD powershell command.

为了获取用户 objectID,我需要自动执行 Connect-AzureAD 操作,为此我使用了以下代码:

In order to get user objectID, I need to automate the operation Connect-AzureAD and for that i used this code:

Connect-AzureAD -TenantId $tenantId  -Verbose
$userObjectID = $(Get-AzureADUser -Filter "UserPrincipalName eq '$Owner'").ObjectId

操作卡在 Connect-AzureAD 上.如何解决这个问题?

The operation stuck at the Connect-AzureAD. how to resolve this?

推荐答案

我找到了解决方案并进行了测试.

I found the solution and test it.

我在 Azure Devops 管道中运行此任务;此任务称为Azure PowerShell 脚本".使用最新安装的版本执行.

I'm running this task in an Azure Devops pipeline; this tasks is called "Azure PowerShell script" executed with the latest installed version.

Install-Module -Name "AzureAD" -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

这篇关于在 Azure Devops 中使用 powershell 自动化 Connect-AzureAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆