如何從Microsoft Entra中的應用中刪除權限

因此,您在Microsoft Entra中擁有所有這些應用程序(企業應用程序和應用程序註冊)。但是您想撤銷應用程序的權限。可以從Microsoft Entra Admin Center的申請中刪除管理員同意權限,而不是用戶同意權限。在本文中,您將學習如何從Microsoft Entra應用程序中刪除管理員和用戶同意權限。

讓我們看一下Microsoft Entra中應用程序的管理員同意和用戶同意權限:

  1. 登錄到微軟Entra管理中心
  2. 擴張身份>應用程序
  3. 選擇企業應用程序(或應用程序註冊
  4. 點擊所有應用程序
  5. 選擇應用
  1. 點擊權限
  2. 選擇管理員同意
  3. 選擇撤銷許可
  1. 單擊用戶同意選項卡
  2. 沒有選擇撤銷許可

因此,當通過管理員同意授予申請時,我們可以從申請中撤銷權限。不幸的是,當通過Microsoft Entra Admin Center授予用戶同意時,不可能撤銷該權限。

在下一步中,我們將仔細研究步驟,並展示如何從PowerShell應用程序中刪除管理員和用戶同意權限。

安裝Microsoft Graph PowerShell模塊

啟動Windows PowerShell作為管理員,然後安裝Microsoft Graph PowerShell。

Install-Module Microsoft.Graph -Force

重要的:在運行CMDLET或腳本以防止錯誤和錯誤結果之前,請務必更新到最新的Microsoft Graph PowerShell模塊版本。

您可以使用下面的腳本刪除兩者用戶和管理員同意許可從應用程序。

查找應用程序對象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腳本。

準備刪除範圍的PowerShell腳本

下載並將remove-apppermissions.ps1 powershell腳本放入C:腳本文件夾。

確保文件未阻止以防止運行腳本時的錯誤。在運行PowerShell腳本時,請在文章中閱讀更多信息。

另一個選擇是將下面的代碼複製並粘貼到記事本中。給它名字刪除申請表1並將其放在C:腳本文件夾。

<#
    .SYNOPSIS
    Remove-AppPermissions.ps1

    .DESCRIPTION
    Remove app permissions from a Microsoft Entra ID application in a tenant.

    .LINK
    www.alitajran.com/remove-permissions-applications/

    .NOTES
    Written by: ALI TAJRAN
    Website:    alitajran.com
    X:          x.com/alitajran
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 11/08/2023 - Initial version
    V1.10, 10/07/2024 - Cleaned up the code
#>

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

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

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

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

$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 "Commencing" $result.DisplayName -ForegroundColor $processMessageColor
    # Get Service Principal using objectId
    $sp = Get-MgServicePrincipal -All | Where-Object { $_.Id -eq $result.Id }
    # Menu selection for User or Admin consent types
    $consentType = [System.Collections.Generic.List[Object]]::new()
    $consentType.Add([PSCustomObject]@{ Name = "Admin consent"; Type = "allprincipals" })
    $consentType.Add([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  "Commencing for" $consentSelect.Name -ForegroundColor $processMessageColor
        # 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 = [System.Collections.Generic.List[Object]]::new()
                foreach ($item in $info) {
                    $usernames.Add((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 $consentSelect.Name "permissions for user" $selectUser.Displayname -ForegroundColor $processMessageColor
                    foreach ($infoScope in $infoScopes) {
                        Write-Host "Resource ID =", $infoScope.ResourceId
                        $assignments = $infoScope.Scope -split " "
                        foreach ($assignment in $assignments) {
                            # Skip empty strings
                            if ($assignment -ne "") {
                                Write-Host "-", $assignment
                            }
                        }
                    }
                    Write-Host "Select items to remove" -ForegroundColor $processMessageColor
                    $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 "Removed consent for $($remove.Scope)" -ForegroundColor $warningMessageColor
                    }
                }
            }
            elseif ($consentSelect.Type -eq "allprincipals") {
                # Admin consent
                $infoScopes = $info | Where-Object { $_.principalId -eq $null }
                Write-Host $consentSelect.Name "permissions" -ForegroundColor $processMessageColor
                foreach ($infoScope in $infoScopes) {
                    Write-Host "Resource ID =", $infoScope.ResourceId
                    $assignments = $infoScope.Scope -split " "
                    foreach ($assignment in $assignments) {
                        # Skip empty strings
                        if ($assignment -ne "") {
                            Write-Host "-", $assignment
                        }
                    }
                }
                Write-Host "Select items to remove" -ForegroundColor $processMessageColor
                $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 "Removed consent for $($remove.Scope)" -ForegroundColor $warningMessageColor
                }
            }
        }
        else {
            Write-Host "No" $consentSelect.Name "permissions found for" $results.DisplayName -ForegroundColor $warningMessageColor
        }
    }
}

Write-Host "Script Finished" -ForegroundColor $systemMessageColor

運行“刪除 - 應用程序” PowerShell腳本

以管理員的身份運行PowerShell並運行下面的命令以啟動remove-apppermissions.ps1 ps腳本。

C:Scripts.Remove-AppPermissions.ps1

網格視圖窗口將在交互式表中顯示輸出。這些都是您的Microsoft Entra租戶中的所有應用程序。

選擇應用然後單擊好的

在我們的示例中,我們將選擇Microsoft Graph命令行工具應用程序

選擇同意類型。允許多個選擇。

在我們的示例中,我們將選擇管理員同意和用戶同意類型。

選擇要刪除應用程序權限的用戶,然後單擊“確定”。

建議閱讀:如何刪除或繞過PDF權限密碼(所有者密碼)

在我們的示例中,我們將選擇兩個用戶。

筆記:如果用戶已獲得管理員同意和用戶同意權限,則將在下一步中提示您兩次。因此,您可以決定是否只想刪除用戶同意,管理員同意或兩者的權限。

它將瀏覽您選擇的管理員同意用戶。選擇要刪除的權限。單擊確定。

它將瀏覽您選擇的用戶同意用戶。選擇要刪除的權限。單擊確定。

在我們的示例中,我們只有1個用戶並選擇。

PowerShell腳本完成了,在PowerShell輸出中,您將看到結果。

驗證ENTRA申請中的權限

轉到申請權限,並確認管理員同意權限和用戶同意權限被撤銷。

這就是管理員同意權限的外觀:

這就是用戶同意權限的外觀:

就是這樣!

結論

您學會瞭如何從Microsoft Entra中的應用程序中刪除權限。您只能從Microsoft Entra Admin Center中刪除管理員同意權限。為了撤銷管理員和用戶同意權限,最好使用“刪除範圍” PowerShell腳本。

您喜歡這篇文章嗎?您可能還喜歡從Microsoft 365中永久刪除用戶。不要忘記關注我們並分享本文。