Set Out of Office Reply with PowerShell and MS GRAPH API

The next Post in my GRAPH API Series will show you how to set the Out of Office Reply to a User.

To learn more from Microsoft GRAPH API, see my Blog Series:
Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams List and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – Some Examples – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

API Reference and Permissions

Let see the following Docs for more Details.

So we need the following Application/Delegated Permission for our Azure App Registration.

  • MailboxSettings.ReadWrite

The Script

$clientID = "your Client ID"
$Clientsecret = "Your Secret"
$tenantID = "your Tenant"


$UPN = "first.last@techguy.at"

$HTMLintern=@"
<html>\n<body>\n<p>I'm at our company's worldwide reunion and will respond to your message as soon as I return.<br>\n</p></body>\n</html>\n
"@

$HTMLextern=@"
<html>\n<body>\n<p>I'm at the Contoso worldwide reunion and will respond to your message as soon as I return.<br>\n</p></body>\n</html>\n
"@


#Function



#Connect to GRAPH API
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}



#Set MailboxSettings
$URLSETOOF = "https://graph.microsoft.com/v1.0/users/$UPN/mailboxSettings" 
#plan
$BodyJsonSETOOF = @"
            {
                
                "automaticRepliesSetting": {
                    "status": "Scheduled",
                    "scheduledStartDateTime": {
                      "dateTime": " 2020-08-25 12:00:00",
                      "timeZone": "UTC"
                    },
                    "scheduledEndDateTime": {
                      "dateTime": " 2021-08-25 12:00:00",
                      "timeZone": "UTC"
                    },
                    "internalReplyMessage": "$HTMLintern",
                    "externalReplyMessage": "$HTMLextern"
                }
            }
"@
#immediately
$BodyJsonSETOOF = @"
            {
                
                "automaticRepliesSetting": {
                    "status": "alwaysEnabled",
                    "internalReplyMessage": "$HTMLintern",
                    "externalReplyMessage": "$HTMLextern"
                }
            }
"@
    
$Result = Invoke-RestMethod -Headers $headers -Body $BodyJsonSETOOF -Uri $URLSETOOF -Method PATCH



The Result

The Out of Office Reply should be set then.

Set Out of Office Reply as Self Service

With au2mator Self Service Portal, you can create a Service and delegate to set an Out of Office Reply.

Set Out of Office Reply with au2mator Self-Service Portal

More Details: www.au2mator.com

GitHub Repo

Here you can find the GitHub Repo with a lot of other examples: Seidlm/Microsoft-Graph-API-Examples (github.com)

Michael Seidl aka Techguy
au2mate everything

Leave a Comment

Your email address will not be published. Required fields are marked *

*