Azure Devops MS 托管代理上的 Get-AzADUser returns 为空
Get-AzADUser returns empty on Azure Devops MS hosted agent
我在 Azure Devops 管道任务上使用 Az Powershell 模块。
我的问题是 Get-AzADUser
命令 returns 在 Azure Devops MS hosted agent
上为空 尽管 它在 [=14 上运行良好=] 和 returns 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\_temp730398-1e70-4056-a1ec-5d9694307da4.ps1'"
2020-10-23T09:04:39.1363993Z ##[command]Import-Module -Name C:\Modules\az_4.7.0\Az.Accounts.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
我应该怎么做才能 returns ADUser 对象?
在 Azure Cloud Shell 中,它使用登录 azure 门户的用户的凭据,如果有效,则意味着您的用户帐户具有列出用户的权限。
在devops管道中,它使用在Project Settings
-> Service connections
中配置的服务主体的凭据,而不是在devops中登录的用户帐户。
要解决此问题,请导航到 devops 中的 Project Settings
-> Service connections
-> 找到您使用的服务连接 -> 单击 Manage Service Principal
。
然后它将打开服务主体的 Azure AD 应用程序页面,导航到 API permissions
-> 添加 应用程序权限 Directory.Read.All
Azure Active Directory Graph
如下(注意:应该是Azure Active Directory Graph
,不是Microsoft Graph
)。
注意:最后别忘了点击Grant admin consent for xxx
按钮。
然后去你的devops试一试,应该可以的。
我在 Azure Devops 管道任务上使用 Az Powershell 模块。
我的问题是 Get-AzADUser
命令 returns 在 Azure Devops MS hosted agent
上为空 尽管 它在 [=14 上运行良好=] 和 returns 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\_temp730398-1e70-4056-a1ec-5d9694307da4.ps1'"
2020-10-23T09:04:39.1363993Z ##[command]Import-Module -Name C:\Modules\az_4.7.0\Az.Accounts.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
我应该怎么做才能 returns ADUser 对象?
在 Azure Cloud Shell 中,它使用登录 azure 门户的用户的凭据,如果有效,则意味着您的用户帐户具有列出用户的权限。
在devops管道中,它使用在Project Settings
-> Service connections
中配置的服务主体的凭据,而不是在devops中登录的用户帐户。
要解决此问题,请导航到 devops 中的 Project Settings
-> Service connections
-> 找到您使用的服务连接 -> 单击 Manage Service Principal
。
然后它将打开服务主体的 Azure AD 应用程序页面,导航到 API permissions
-> 添加 应用程序权限 Directory.Read.All
Azure Active Directory Graph
如下(注意:应该是Azure Active Directory Graph
,不是Microsoft Graph
)。
注意:最后别忘了点击Grant admin consent for xxx
按钮。
然后去你的devops试一试,应该可以的。