首页 > 解决方案 > 通过 PowerShell 检索 Azure AD 应用程序的“API 权限”

问题描述

出于报告和监视目的,我是否喜欢检索 Azure 门户中显示的“API 权限”应用程序(应用程序注册)信息。

我试过下面的代码

$app = Get-AzureADApplication -ObjectId 'aa7e174d-2639-4ac7-9b11-6799466c3c9b'
$app.Oauth2Permissions

但这只会产生以下信息:

AdminConsentDescription : Allow the application to access foobar_HVV on behalf of the signed-in user.
AdminConsentDisplayName : Access foobar_HVV
Id                      : h1285f9d5-b00d-4bdb-979d-c4d6487fa000
IsEnabled               : True
Type                    : User
UserConsentDescription  : Allow the application to access foobar_HVV on your behalf.
UserConsentDisplayName  : Access foobar_HVV
Value                   : user_impersonation

但是应用程序“foobar_HVV”的“API 权限”显示了完全不同的权限。特别是我的报告需要每个权限的“Typ”(代表,应用程序)和“状态”。

标签: azurepowershellazure-active-directory

解决方案


如果你想得到API permissions,你需要使用下面的命令。

$app = Get-AzureADApplication -ObjectId '<object-id of the App Registration>'
$app.requiredResourceAccess | ConvertTo-Json -Depth 3

在此处输入图像描述

ResourceAppId是API的Application ID服务主体的,例如Microsoft GraphResourceAccess包括您添加到应用程序的权限,Scope表示Delegated permissionRole表示Application permission.

我的 API 权限:

在此处输入图像描述

要查看 API 权限的详细信息,您需要使用以下命令。例如,我们想知道截图中权限的详细信息,它是Id,所以我们需要使用.5b567255-7703-4780-807c-7be8301ae99bTypeRole$sp.AppRoles

$sp = Get-AzureADServicePrincipal -All $true | Where-Object {$_.AppId -eq '00000003-0000-0000-c000-000000000000'}
$sp.AppRoles | Where-Object {$_.Id -eq '5b567255-7703-4780-807c-7be8301ae99b'}

在此处输入图像描述

如果要获取Delegated permission( Typeis Scope),我们需要使用:

$sp = Get-AzureADServicePrincipal -All $true | Where-Object {$_.AppId -eq '00000003-0000-0000-c000-000000000000'}
$sp.Oauth2Permissions | Where-Object {$_.Id -eq 'e1fe6dd8-ba31-4d61-89e7-88639da4683d'}

在此处输入图像描述

要检查Status,没有直接的方法,您需要检查服务主体的管理员授予的权限对应于您的 AAD 租户中的 AD App。

首先,获取服务主体 $appsp

$app = Get-AzureADApplication -ObjectId '<object-id of the App Registration>'
$appsp = Get-AzureADServicePrincipal -All $true | Where-Object {$_.AppId -eq $app.AppId}

获取Delegated permissions已被授予的(StatusGranted):

Get-AzureADServicePrincipalOAuth2PermissionGrant -ObjectId $appsp.ObjectId -All $true | ConvertTo-Json

在此处输入图像描述

ResourceIdObject IdAPI 的服务主体:

在此处输入图像描述

获取Application permissions已被授予的(StatusGranted):

Get-AzureADServiceAppRoleAssignedTo -ObjectId $appsp.ObjectId | ConvertTo-Json

Id是第一个屏幕截图中的IdResourceAccess

在此处输入图像描述

如果权限没有被授予( Statusis Not Granted),你将无法通过上面的命令获得权限。

比如我Application permission在portal中添加了一个新的,然后再次运行命令,我们仍然可以得到已经授予的权限。

在此处输入图像描述

在此处输入图像描述


推荐阅读