网站搜索

如何从 Microsoft Entra 中的应用程序删除权限


因此,您在 Microsoft Entra 中拥有所有这些应用程序(企业应用程序和应用程序注册)。但您想撤销应用程序的权限。可以从 Microsoft Entra 管理中心的应用程序中删除管理员同意权限,但不能删除用户同意权限。在本文中,您将了解如何从 Microsoft Entra 应用程序中删除管理员和用户同意权限。

在 Microsoft Entra 中查找应用程序权限

让我们看看 Microsoft Entra 中应用程序的管理员同意和用户同意权限:

  1. 登录 Microsoft Entra 管理中心

  2. 展开身份 > 应用程序

  3. 选择企业应用程序(或应用程序注册)

  4. 点击所有应用程序

  5. 选择应用程序

  1. 点击权限

  2. 选择管理员同意

  3. 选择撤消权限

  1. 单击用户同意选项卡

  2. 没有撤销权限的选项

因此,当通过管理员同意授予应用程序权限时,我们可以撤销应用程序的权限。不幸的是,当通过 Microsoft Entra 管理中心的用户同意授予权限时,无法撤销权限。

在下一步中,我们将完成这些步骤并展示如何使用 PowerShell 从应用程序中删除管理员和用户同意权限。

安装 Microsoft Graph PowerShell 模块

以管理员身份启动 Windows PowerShell 并安装 Microsoft Graph PowerShell。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

从应用程序中删除用户和管理员同意权限

您可以使用下面的脚本从应用程序中删除用户和管理员同意权限。

在“概述”选项卡中找到应用程序对象 ID。接下来,将其粘贴到第 4 行。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Application.ReadWrite.All", "DelegatedPermissionGrant.ReadWrite.All"

# Get Service Principal using objectId
$sp = Get-MgServicePrincipal -ServicePrincipalId 453d37f9-20e5-4325-bc00-67d1581a0232

# Get all delegated permissions for the service principal
$spOAuth2PermissionsGrants = Get-MgServicePrincipalOauth2PermissionGrant -ServicePrincipalId $sp.Id -All

# Remove all delegated permissions
$spOAuth2PermissionsGrants | ForEach-Object {
    Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $_.Id
}

从应用程序中删除管理员同意权限

仅从应用程序中删除管理员同意权限。

在“概述”选项卡中找到应用程序对象 ID。接下来,将其粘贴到第 4 行。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Application.ReadWrite.All", "DelegatedPermissionGrant.ReadWrite.All"

# Get Service Principal using objectId
$sp = Get-MgServicePrincipal -ServicePrincipalId 453d37f9-20e5-4325-bc00-67d1581a0232

# Get all delegated permissions for the service principal
$spOAuth2PermissionsGrants = Get-MgServicePrincipalOauth2PermissionGrant -ServicePrincipalId $sp.Id -All

# Remove only delegated permissions granted with admin consent
$spOAuth2PermissionsGrants | Where-Object { $_.ConsentType -eq "AllPrincipals" } | ForEach-Object {
    Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $_.Id
}

从应用程序中删除用户同意权限

仅从应用程序中删除用户同意权限。

在“概述”选项卡中找到应用程序对象 ID。接下来,将其粘贴到第 4 行。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Application.ReadWrite.All", "DelegatedPermissionGrant.ReadWrite.All"

# Get Service Principal using objectId
$sp = Get-MgServicePrincipal -ServicePrincipalId 453d37f9-20e5-4325-bc00-67d1581a0232

# Get all delegated permissions for the service principal
$spOAuth2PermissionsGrants = Get-MgServicePrincipalOauth2PermissionGrant -ServicePrincipalId $sp.Id -All

# Remove only delegated permissions granted with user consent
$spOAuth2PermissionsGrants | Where-Object { $_.ConsentType -ne "AllPrincipals" } | ForEach-Object {
    Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $_.Id
}

让我们看看使用 PowerShell 脚本删除 Microsoft Entra 应用程序权限的更好方法。

使用 Powershell 脚本删除 Entra ID 应用程序权限

删除用户和管理员同意权限的一个好方法是使用 PowerShell 脚本。

准备Remove-AppPermissions PowerShell脚本

下载Remove-AppPermissions.ps1 PowerShell 脚本并将其放置在C:\scripts 文件夹中。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Remove-AppPermissions.ps1 并将其放置在 C:\scripts 文件夹中。

# Variables
$systemMessageColor = "cyan"
$processMessageColor = "green"
$errorMessageColor = "red"
$warningMessageColor = "yellow"

Write-Host -ForegroundColor $systemMessageColor "Script started`n"
Write-Host "--- Script to delete app permissions from an Entra ID application in a tenant ---"

Write-Host -ForegroundColor $processMessageColor "`nChecking for Microsoft Graph PowerShell module"
if (Get-Module -ListAvailable -Name Microsoft.Graph.Authentication) {
    Write-Host -ForegroundColor $processMessageColor "Microsoft Graph PowerShell module found"
}
else {
    Write-Host -ForegroundColor $warningMessageColor -BackgroundColor $errorMessageColor "Microsoft Graph PowerShell Module not installed. Please install and re-run the script`n"
    Write-Host "You can install the Microsoft Graph PowerShell module by:`n"
    Write-Host "    1. Launching an elevated PowerShell console then,"
    Write-Host "    2. Running the command, 'Install-Module -Name Microsoft.Graph'.`n"
    Pause ## Pause to view error on screen
    exit 0 ## Terminate script
}

Connect-MgGraph -Scopes "User.ReadWrite.All", "Application.ReadWrite.All", "DelegatedPermissionGrant.ReadWrite.All"

$results = Get-MgServicePrincipal -All | Select-Object Id, AppId, DisplayName | Sort-Object DisplayName | Out-GridView -PassThru -Title "Select Application (Multiple selections permitted)"
foreach ($result in $results) {
    # Loop through all selected options
    Write-Host -ForegroundColor $processMessageColor "Commencing", $result.DisplayName
    # Get Service Principal using objectId
    $sp = Get-MgServicePrincipal -All | Where-Object { $_.Id -eq $result.Id }
    # Menu selection for User or Admin consent types
    $consentType = @()
    $consentType += [PSCustomObject]@{ Name = "Admin consent"; Type = "allprincipals" }
    $consentType += [PSCustomObject]@{ Name = "User consent"; Type = "principal" }
    $consentSelects = $consentType | Out-GridView -PassThru -Title "Select Consent type (Multiple selections permitted)"

    foreach ($consentSelect in $consentSelects) {
        # Loop through all selected options
        Write-Host -ForegroundColor $processMessageColor "Commencing for", $consentSelect.Name
        # Get all delegated permissions for the service principal
        $spOAuth2PermissionsGrants = Get-MgOauth2PermissionGrant -All | Where-Object { $_.clientId -eq $sp.Id }
        $info = $spOAuth2PermissionsGrants | Where-Object { $_.consentType -eq $consentSelect.Type }
        
        if ($info) {
            # If there are permissions set
            if ($consentSelect.Type -eq "principal") {
                # User consent
                $usernames = @()
                foreach ($item in $info) {
                    $usernames += Get-MgUser -UserId $item.PrincipalId
                }
                $selectUsers = $usernames | Select-Object Displayname, UserPrincipalName, Id | Sort-Object Displayname | Out-GridView -PassThru -Title "Select Consent type (Multiple selections permitted)"
                foreach ($selectUser in $selectUsers) {
                    # Loop through all selected options
                    $infoScopes = $info | Where-Object { $_.principalId -eq $selectUser.Id }
                    Write-Host -ForegroundColor $processMessageColor "`n"$consentSelect.Name, "permissions for user", $selectUser.Displayname
                    foreach ($infoScope in $infoScopes) {
                        Write-Host "`nResource ID =", $infoScope.ResourceId
                        $assignments = $infoScope.Scope -split " "
                        foreach ($assignment in $assignments) {
                            Write-Host "-", $assignment
                        }
                    }
                    Write-Host -ForegroundColor $processMessageColor "`nSelect items to remove`n"
                    $removes = $infoScopes | Select-Object Scope, ResourceId, Id | Out-GridView -PassThru -Title "Select permissions to delete (Multiple selections permitted)"
                    foreach ($remove in $removes) {
                        Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $remove.Id
                        Write-Host -ForegroundColor $warningMessageColor "Removed consent for", $remove.Scope
                    }
                }
            } 
            elseif ($consentSelect.Type -eq "allprincipals") {
                # Admin consent
                $infoScopes = $info | Where-Object { $_.principalId -eq $null }
                Write-Host -ForegroundColor $processMessageColor $consentSelect.Name, "permissions"
                foreach ($infoScope in $infoScopes) {
                    Write-Host "`nResource ID =", $infoScope.ResourceId
                    $assignments = $infoScope.Scope -split " "
                    foreach ($assignment in $assignments) {
                        Write-Host "-", $assignment
                    }
                }
                Write-Host -ForegroundColor $processMessageColor "`nSelect items to remove`n"
                $removes = $infoScopes | Select-Object Scope, ResourceId, Id | Out-GridView -PassThru -Title "Select permissions to delete (Multiple selections permitted)"
                foreach ($remove in $removes) {
                    Remove-MgOauth2PermissionGrant -OAuth2PermissionGrantId $remove.Id
                    Write-Host -ForegroundColor $warningMessageColor "Removed consent for", $remove.Scope
                }
            }
        }
        else {
            Write-Host -ForegroundColor $warningMessageColor "`nNo", $consentSelect.Name, "permissions found for" , $results.DisplayName, "`n"
        }
    }
}

Write-Host -ForegroundColor $systemMessageColor "`nScript Finished"

运行Remove-AppPermissions PowerShell 脚本

以管理员身份运行 PowerShell 并运行以下命令来启动 Remove-AppPermissions.ps1 PS 脚本。

C:\Scripts\.\Remove-AppPermissions.ps1

网格视图窗口将在交互式表格中显示输出。这些是 Microsoft Entra 租户中的所有应用程序。

选择应用程序,然后单击确定。

在我们的示例中,我们将选择 Microsoft Graph 命令行工具应用。

选择同意类型。允许多项选择。

在我们的示例中,我们将选择管理员同意和用户同意类型。

选择要从应用程序中删除权限的用户,然后单击“确定”。

在我们的示例中,我们将选择这两个用户。

注意:如果用户拥有管理员同意和用户同意权限,则下一步会提示您两次。因此,您可以决定是否只想删除用户同意、管理员同意或同时删除用户的权限。

它将通过您选择的管理员同意的用户。选择要删除的权限。单击“确定”。

它将遍历您选择的用户同意的用户。选择要删除的权限。单击“确定”。

在我们的示例中,我们只有 1 个用户并选择该用户。

PowerShell 脚本完成,您将在输出中看到结果。

这就是它的样子。

Script started

--- Script to delete app permissions from an Entra ID application in a tenant ---

Checking for Microsoft Graph PowerShell module
Microsoft Graph PowerShell module found
Welcome to Microsoft Graph!

Connected via delegated access using 12d82ffa-202b-4c2f-a7e8-296a70dab67e
Readme: https://aka.ms/graph/sdk/powershell
SDK Docs: https://aka.ms/graph/sdk/powershell/docs
API Docs: https://aka.ms/graph/docs

NOTE: You can use the -NoWelcome parameter to suppress this message.

Commencing Microsoft Graph Command Line Tools
Commencing for User consent

 User consent permissions for user Admin 2

Resource ID = 73d0154b-e490-44fe-9447-fa47ec7fdd7f
-
- User.Read.All
- Group.ReadWrite.All
- Application.ReadWrite.All
- DelegatedPermissionGrant.ReadWrite.All
- openid
- profile
- offline_access

Select items to remove

Removed consent for  User.Read.All Group.ReadWrite.All Application.ReadWrite.All DelegatedPermissionGrant.ReadWrite.All openid profile offline_access

 User consent permissions for user Admin Tajran

Resource ID = 73d0154b-e490-44fe-9447-fa47ec7fdd7f
-
- User.Read.All
- Group.ReadWrite.All
- Application.ReadWrite.All
- DelegatedPermissionGrant.ReadWrite.All
- openid
- profile
- offline_access

Select items to remove

Removed consent for  User.Read.All Group.ReadWrite.All Application.ReadWrite.All DelegatedPermissionGrant.ReadWrite.All openid profile offline_access
Commencing for Admin consent
Admin consent permissions

Resource ID = 73d0154b-e490-44fe-9447-fa47ec7fdd7f
- User.Read

Select items to remove

Removed consent for User.Read

Script Finished

验证 Entra 应用程序中的权限

转到应用程序权限并确认管理员同意权限和用户同意权限已被撤销。

这是查找管理员同意权限的方式:

这是查找用户同意权限的方式:

就是这样!

了解更多:使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户 »

结论

您了解了如何从 Microsoft Entra 中的应用程序删除权限。您只能从 Microsoft Entra 管理中心删除管理员同意权限。要撤销管理员和用户同意权限,最好使用Remove-AppPermissions PowerShell 脚本。

您喜欢这篇文章吗?您可能还喜欢从 Microsoft 365 中永久删除用户。不要忘记关注我们并分享这篇文章。

相关文章