What It Does
The script connects to Exchange Online as your app. For the one mailbox in Mailbox and the one user or mail-enabled security group in Delegate, it does what Operation says, Grant by default or Remove, for the kind of access in Access: FullAccess, SendAs or SendOnBehalf.
It checks first. Granting access the person already has, or removing access the person does not have, changes nothing and ends OK saying it is already so. AutoMapping, on by default, decides whether the mailbox is added to the person's Outlook automatically when Full Access is granted. It applies only to granting Full Access, and a group does not get automapping.
If the person already has Full Access, the automapping setting cannot be changed in place. The way is to remove the permission and grant it again with the new setting.
It never touches any other permission, never grants access to more than one person per run and never changes folder or calendar permissions. Afterwards it reads the access back, retrying for a short while because Exchange shows a change late, and ends with an error if the access is not as asked. Use DryRun=true to see the change first. If the ExchangeOnlineManagement module is not installed, the run is skipped.
To see what access exists today, run get-mailbox-permissions.
Before You Run It
- Register an app for this client with a certificate, and keep the certificate with its private key on the computer that runs the script.
- Give the app the Microsoft Graph application permission Organization.Read.All and the Exchange.ManageAsApp permission.
- Assign the app an Exchange role that can change mailbox permissions, for example Exchange Administrator. A read-only role is not enough.
- Install the ExchangeOnlineManagement module (version 3.1 or later) on the computer.
- Run it once with DryRun=true and read the line it would change.
- Decide on AutoMapping before you grant Full Access to a person who will see a very large mailbox.
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: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Adds or removes one FullAccess, SendAs or SendOnBehalf permission for one user on one mailbox.
- Restart: It never restarts the machine.
- Typical run: Under 1 minute.
- Example answer: "Granted FullAccess on support@contoso.com to jane@contoso.com (automapping off)"
- 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.
- Mailbox: The mailbox to change access on (its address or UPN). Required.
- Delegate: Who gets or loses the access (a user's address or UPN, or a mail-enabled security group's address). Required.
- Access: FullAccess (default), SendAs or SendOnBehalf.
- Operation: Grant (default) or Remove.
- AutoMapping: true (default) to let Outlook add the mailbox automatically when FullAccess is granted; false to stop that. Only used by Grant with FullAccess.
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.