What It Does
The script signs in to one client tenant as your app registration and reads every account with userType Guest, with its invitation state and its sign-in activity. A guest is listed when the invitation is still pending and older than StaleDays, when the newest of its sign-in times is older than StaleDays, or when it has no sign-in on record and the account itself is older than StaleDays.
Each row shows the guest, the reason, the days idle, the last sign-in and whether the account is still enabled. Guests in ExcludeGuests are skipped. The answer counts the stale guests out of all guests.
Sign-in activity needs Microsoft Entra ID P1 and the AuditLog.Read.All permission. Without them the script warns and judges only unaccepted invitations, rather than guessing about idle guests. Microsoft does not keep sign-in times from before April 2020.
Partly tested: it ran against a test tenant, where the guest query and the sign-in check worked, but that tenant had no guest old enough to be stale. It keeps no state, so run it monthly and review the list.
Before You Run It
- Create an app registration in the client tenant with the Graph application permissions User.Read.All and AuditLog.Read.All, and give admin consent.
- Sign-in activity needs Entra ID P1 or P2 in the tenant (Microsoft 365 Business Premium includes P1).
- Put partner accounts you must keep in ExcludeGuests.
Type and Risk
- Type: Report. It collects facts into a table and never alerts. It exits 0, or 2 when it could not run.
- Risk: Low. It reads and changes nothing.
- Restart: It never restarts the machine.
- Typical run: Under 1 minute.
- Example answer: "12 stale guest(s) of 40 (no sign-in or invitation accepted for 90 days)"
- Partly tested before listing: run with Windows PowerShell 5.1 against a Microsoft 365 test tenant as far as it allowed, and compared with tested scripts. The help says which part is unproven.
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).
- StaleDays: A guest with no sign-in, or an unaccepted invitation, for this many days is stale. Whole number, 14 to 3650. Default 90.
- ExcludeGuests: Guests to leave out, by sign-in name or mail address, separated by comma, semicolon or new line. Default empty.
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.