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

查看:251
本文介绍了在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

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

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