Skip to content

Instantly share code, notes, and snippets.

@kspeeckaert
Last active September 20, 2021 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kspeeckaert/74fb9531b9c1f177a290904edd137fc7 to your computer and use it in GitHub Desktop.
Save kspeeckaert/74fb9531b9c1f177a290904edd137fc7 to your computer and use it in GitHub Desktop.
Activate out of office using PowerShell
#Requires -Module ExchangeOnlineManagement
#Requires -Module BurntToast
$UPN = 'john.appleseed@apple.com'
$DateFormatCulture = [CultureInfo]'en-GB'
# This has to be at the top, before the calendar, to avoid the 'single thread apartment' error
Write-Host 'Connecting to Exchange Online...'
Connect-ExchangeOnline -UserPrincipalName $UPN
# Taken from: https://www.sapien.com/blog/2009/08/24/writing-form-centered-scripts-with-primalforms/
$DatesOOO = Invoke-Expression ".\Get-GuiDate.ps1 -DisplayMode 1 -FirstDayofWeek Monday -WeekNumbers -Title 'Select Out Of Office dates'"
$StartDateTime = $DatesOOO[0]
$EndDateTime = $DatesOOO[-1].AddDays(1).AddSeconds(-1)
$OooMessageInternal =
@"
<html>
<body>
<div>
<p>
Hello,
</p>
<p>
As you might have noticed, I am not in the office! I'll be back on <b>$($EndDateTime.AddSeconds(1).ToString('dddd, MMMM dd yyyy', $DateFormatCulture))</b>.
</p>
<p>
See you!<br>
John
</p>
</div>
</body>
</html>
"@
$OooMessageExternal =
@"
<html>
<body>
<div>
<p>
Hello,
</p>
<p>
I am currently out of the office, returning <b>$($EndDateTime.AddSeconds(1).ToString('dddd, MMMM dd yyyy', $DateFormatCulture))</b>.
</p>
<p>
Best regards,<br>
John Appleseed
</p>
</div>
</body>
</html>
"@
try {
Set-MailboxAutoReplyConfiguration `
-Identity $UPN `
-AutoDeclineFutureRequestsWhenOOF $true `
-AutoReplyState Scheduled `
-StartTime $StartDateTime.ToUniversalTime() `
-EndTime $EndDateTime.ToUniversalTime() `
-ExternalMessage $OooMessageExternal `
-InternalMessage $OooMessageInternal
$MbxConfig = Get-MailboxAutoReplyConfiguration -Identity $UPN
New-BurntToastNotification `
-AppLogo outlook.png `
-Text "Out of Office activated!",
"From $($MbxConfig.StartTime.ToString('dddd, MMMM dd', $DateFormatCulture)) until $($MbxConfig.EndTime.ToString('dddd, MMMM dd', $DateFormatCulture)) (included)"
}
finally {
Disconnect-ExchangeOnline -Confirm:$false | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment