What It Does
The script signs in to one tenant with the app, using the certificate (preferred) or the client secret from the environment. It reads the access token it gets back and decodes the permissions inside it on this computer. The token is never printed, logged or written. It changes nothing in the tenant.
It writes one row for the certificate, one for the sign-in and one for each granted permission. It ends ALERT when the sign-in works but the token carries no permissions at all, when a permission named in RequiredPermissions is missing, or when the certificate expires within CertificateWarnDays. A secret has no readable expiry, so a client secret is reported as information only.
With CheckExchange set to true it also connects to Exchange Online and reads the accepted domains, and alerts if that connection fails. If the ExchangeOnlineManagement module is missing, the Exchange row says SKIPPED and the rest still runs. Exchange.ManageAsApp is checked against the Exchange token when you list it.
A sign-in that fails outright, for example because of a wrong tenant or app ID, ends ERROR with the service's message. A sign-in that succeeds with no Graph permissions ends ALERT. Run it again after you fix the app and it re-checks everything from scratch.
Before You Run It
- Register the app in the client's Entra tenant, upload the certificate and grant admin consent for the permissions your other scripts need.
- Install the certificate with its private key on this computer, in LocalMachine\My or CurrentUser\My, and pass its thumbprint.
- List what your scripts need in RequiredPermissions, for example Mail.Read or Exchange.ManageAsApp, so a gap alerts.
- For CheckExchange, install ExchangeOnlineManagement 3.1 or later and give the app Organization.Read.All, Exchange.ManageAsApp and an Exchange role such as Global Reader.
- Schedule it monthly from your RMM. Alert on exit 1 to catch an expiring certificate early.
Type and Risk
- Type: Monitor. It checks one condition. It exits 1 when it finds the problem, so your RMM can alert on it, 0 when all is well, and 2 when it could not run.
- Risk: Low. It reads and changes nothing.
- Restart: It never restarts the machine.
- Typical run: Under 30 seconds.
- Example answer: "App access: 4 permission(s) granted; certificate expires in 18 day(s) (limit 30); missing Mail.Read"
- 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's tenant ID (a GUID) or domain such as contoso.onmicrosoft.com. Required.
- ClientId: The app registration's Application (client) ID, a GUID. Required.
- CertificateThumbprint: Thumbprint of the app's certificate, with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. Leave empty to use the ClientSecret environment variable.
- CertificateWarnDays: Alert when the certificate expires within this many days. Whole number, 1 to 3650. Default 30.
- CheckExchange: true to also connect to Exchange Online and run Get-AcceptedDomain. Default false.
- RequiredPermissions: Permissions the scripts you plan to run need, separated by comma, semicolon or new line (for example "Mail.Read, User.Read.All, Exchange.ManageAsApp"). Each missing one alerts. Default none (only the list is shown).
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.