What It Does
The script signs in as your app and sends one email as FromMailbox through Microsoft Graph, to the addresses in To, with your Subject and MessageBody. The body is plain text unless BodyFormat is Html. It sends nothing else and reads no mailbox. The message is saved in the sender's Sent Items.
If you give AttachmentPath, it checks the file exists and is under 3 MB, and attaches it inside the same call. A file of 3 MB or more is refused with a clear message, because larger files need a different method that this script does not use.
With DryRun=true it lists the sender, recipients, shortened subject and attachment name, and sends nothing. It does not print the body. A real run ends CHANGED when Graph accepts the message. Use a message trace to check delivery. Running it again sends another copy.
The Mail.Send permission lets the app send as any mailbox in the tenant, so limit the app to the sending mailbox with an Exchange application access policy or RBAC for Applications. Otherwise a leaked certificate could send mail as anyone, including the CEO.
Before You Run It
- Register the app with the Graph application permission Mail.Send and grant admin consent.
- Limit the app to the one sending mailbox with an Exchange application access policy or RBAC for Applications.
- Use a mailbox that exists and is licensed, or a shared mailbox, as FromMailbox.
- Do a dry run first, then send one real message to yourself.
- Keep attachments under 3 MB, or send a link instead.
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: Sends one email (saved in the sender's Sent Items).
- Restart: It never restarts the machine.
- Typical run: Under 30 seconds.
- Example answer: "Sent 'Backup failed' from [sender mailbox] to 2 recipient(s); Graph accepted it"
- 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 app registration's Application (client) ID, a GUID. Required.
- CertificateThumbprint: Thumbprint (40 hex characters) of the app's certificate with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. When empty, the secret is read from the environment variable ClientSecret (a secure RMM field).
- FromMailbox: The sending mailbox, such as [sender mailbox]. Required.
- To: Recipient addresses, separated by comma, semicolon or new line. Required.
- Subject: Subject line. Required.
- MessageBody: Message text. Required. Plain text unless BodyFormat is Html.
- BodyFormat: Text (default) or Html.
- AttachmentPath: Path of one file to attach, under 3 MB. Empty (default) sends no attachment.
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.