Get AzADUser在Azure Devops MS托管代理上返回空

Get AzADUser在Azure Devops MS托管代理上返回空,azure,azure-devops,azure-active-directory,azure-pipelines,azure-powershell,Azure,Azure Devops,Azure Active Directory,Azure Pipelines,Azure Powershell,我正在Azure Devops管道任务中使用Az Powershell模块。 我的问题是Get azuduser命令在Azure Devops MS hosted agent上返回空的,尽管它在Azure Cloud Shell上运行良好,并返回AzAD用户。我对两者使用相同的帐户(Cloud Shell和Azure Devops) 下面是Azure Devops任务的日志: 2020-10-23T09:04:34.5200816Z ##[section]Starting: Azure Powe

我正在Azure Devops管道任务中使用Az Powershell模块。 我的问题是
Get azuduser
命令
Azure Devops MS hosted agent
上返回空的
,尽管它在
Azure Cloud Shell
上运行良好,并返回AzAD用户。我对两者使用相同的帐户(Cloud Shell和Azure Devops)

下面是Azure Devops任务的日志:

2020-10-23T09:04:34.5200816Z ##[section]Starting: Azure PowerShell script: FilePath
2020-10-23T09:04:34.5561322Z ==============================================================================
2020-10-23T09:04:34.5562144Z Task         : Azure PowerShell
2020-10-23T09:04:34.5563006Z Description  : Run a PowerShell script within an Azure environment
2020-10-23T09:04:34.5563312Z Version      : 5.176.2
2020-10-23T09:04:34.5564072Z Author       : Microsoft Corporation
2020-10-23T09:04:34.5565422Z Help         : https://aka.ms/azurepowershelltroubleshooting
2020-10-23T09:04:34.5566084Z ==============================================================================
2020-10-23T09:04:36.0669385Z Generating script.
2020-10-23T09:04:36.1560120Z ========================== Starting Command Output ===========================
2020-10-23T09:04:36.1824475Z ##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\11730398-1e70-4056-a1ec-5d9694307da4.ps1'"
2020-10-23T09:04:39.1363993Z ##[command]Import-Module -Name C:\Modules\az_4.7.0\Az.Accounts\1.9.4\Az.Accounts.psd1 -Global
2020-10-23T09:04:48.4455502Z ##[command]Clear-AzContext -Scope Process
2020-10-23T09:04:49.5325261Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2020-10-23T09:04:49.8445719Z ##[command]Connect-AzAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope
2020-10-23T09:04:52.0059255Z ##[command] Set-AzContext -SubscriptionId *** -TenantId ***
2020-10-23T09:04:52.9718588Z Get-AzADUser

我应该怎么做才能让它返回ADUser对象?

在Azure Cloud Shell中,它使用登录Azure门户的用户的凭据,如果它有效,则意味着您的用户帐户有权列出用户

在devops管道中,它使用在
项目设置中配置的服务主体的凭据,而不是在devops中登录的用户帐户

要解决此问题,请导航到devops中的
项目设置
服务连接
->查找您使用的服务连接->单击
管理服务主体

然后,它将打开服务主体的Azure AD应用程序页面,导航到
API权限
->添加应用程序权限
目录。阅读
Azure Active Directory图形的所有
,如下所示(:它应该是
Azure Active Directory图形
,而不是
Microsoft图形

注意:最后别忘了点击
授予xxx管理员许可
按钮

然后去你的devops试一试,应该可以