What It Does
This script is a High-risk Action. It finds the same high-risk consents as test-risky-app-consents and removes them. A delegated consent that contains a high-risk scope is deleted whole: the app loses every delegated permission of that grant on that resource, not only the risky one, for the people it covers. An application permission on the list is deleted on its own, and that app's other permissions stay.
It is a dry run until DryRun=false and Approve=remove-risky-app-consents are both set. The dry run lists every removal and changes nothing. It refuses to remove more than MaxChanges (default 25) in one run. Before the first removal it saves the grant and assignment IDs, the app, the flagged permissions and who the grant covers, with no secrets, as JSON under the MSP Outpost backups folder and names the file. Use it to identify what to re-consent. The backup lists the flagged scopes, which may be only part of a deleted delegated grant.
It never deletes the app, its service principal, its secrets or its users, and never disables an app. It skips Microsoft's own apps, the app it signs in as and every app in AllowedApps. OnlyApps limits a run to the apps you name. After the removal it checks again and warns if a consent still shows, since Graph can lag a few minutes.
Users of a removed app may be asked for consent again, and an app they still need stops working until you add it to AllowedApps and consent again. Access tokens already issued stay valid until they expire. A second run finds nothing and reports it is already clean.
Before You Run It
- Run test-risky-app-consents and read every app it lists.
- Create an app registration with Directory.Read.All, Application.Read.All, DelegatedPermissionGrant.ReadWrite.All and AppRoleAssignment.ReadWrite.All, with admin consent.
- Put every app people rely on in AllowedApps before you run it for real.
- Run the dry run first and read the list. Use OnlyApps to start with a single app.
- Run it with DryRun=false and Approve=remove-risky-app-consents, and keep the before-state file.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DryRun=false and Approve=remove-risky-app-consents, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
- What it changes: Deletes delegated permission grants and application permission assignments that contain high-risk permissions.
- Restart: It never restarts the machine.
- Typical run: Under 2 minutes.
- Example answer: "Removed 5 high-risk consent(s) from 3 app(s)"
- Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- TenantId: The client tenant: its ID (a GUID) or a domain such as contoso.onmicrosoft.com. Required.
- ClientId: The Application (client) ID of the app registration that holds the Graph permissions. Required.
- CertificateThumbprint: Thumbprint of the app's certificate (private key in LocalMachine\My or CurrentUser\My). Preferred. When empty, the app secret is read from the environment variable ClientSecret (a secure/hidden RMM field; never an argument).
- AllowedApps: Apps to leave alone, by application (client) ID or exact display name, separated by comma, semicolon or new line. Default empty.
- OnlyApps: When set, remove only from these apps (application ID or exact display name). Default empty (every risky app not allowed).
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.