7

Automate Out of Office scheduling using PowerShell

 3 years ago
source link: https://www.powershell.no/outlook,exchange,powershell/2017/11/05/automate-out-of-office-schedule.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Challenge

If you have an Exchange mailbox, you have the option to configure Out of Office messages when you are away from work:

alt

Usually, the default options is sufficient: You configure a message, a time range, and you are all set.
In my scenario, I am having a paternity leave for the next 20 weeks where I will work 50% during the work week. This means I will have to turn Out of Office on and off several times every week.

Solution

This is a perfect opportunity to look into what options we have as end users when working with PowerShell against Exchange - whether it is Exchange Online or an on-premises such as Exchange Server 2016.

The article Connect to Exchange Online PowerShell outlines the necessary steps. Let us try this using a regular user with no administrative permissions and see which commands we have access to:

# Credential for the user mailbox to configure Out-of-office schedule for $UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

$EXOModule = Import-PSSession $Session

Get-Command -Module $EXOModule.Name Get-Command -Module $EXOModule.Name | Format-Wide -Property Name -AutoSize

Sample output:

alt

That is quite a lot of commands, and many of them can be very useful. For example, we can change our photo using Get/Set-UserPhoto as well as view and clean up mobile devices using Get/Set-MobileDevice.

What we are looking for is Set-MailboxAutoReplyConfiguration, which can perform the task we want to automate:

# Credential for the user mailbox to configure Out-of-office schedule for $UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

$EXOModule = Import-PSSession $Session

# Customize as needed - for example, setup as parameters for a function/script $StartTime = Get-Date 08:00 $EndTime = Get-Date 16:00

# AutoReplyState = Enabled/Disabled/Scheduled Set-MailboxAutoReplyConfiguration -Identity [email protected] -StartTime $StartTime -EndTime $EndTime -AutoReplyState Enabled

# Cleanup Remove-Module -Name $EXOModule.Name

Remove-PSSession $Session

Sample output:

alt

We can also verify the result:

alt

The last piece of the puzzle is to schedule this script to run on the desired days of week.
One option is to run it as a scheduled task on a local computer, but I have chosen to run it in Azure Automation in order to avoid relying on a computer being powered on. You can get an Azure Automation account with up to 500 minutes of runbook run time each month, which should cover the needs in this example.

Sample runbook:

param ( [string]$UserName )

# Credential for the user mailbox to configure Out-of-office schedule for $UserCredential = Get-AutomationPSCredential -Name $UserName

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

$EXOModule = Import-PSSession $Session

# One hour earlier due to different time zone on the server side $StartTime = Get-Date 07:00 $EndTime = Get-Date 15:00

# AutoReplyState = Enabled/Disabled/Scheduled Set-MailboxAutoReplyConfiguration -Identity $UserName -StartTime $StartTime -EndTime $EndTime -AutoReplyState Scheduled

# Cleanup Remove-Module -Name $EXOModule.Name

Remove-PSSession $Session

The runbook can then the attached to a custom schedule in order to make it run on the week days we want:

alt
alt

Summary

In this article we have looked at how to automate the configuration of Out of Office scheduling for an Exchange mailbox using PowerShell and Azure Automation.
I am also using an Azure Automation runbook with the opposite schedule as for the Out of Office scheduling runbook for starting the climate control in my car. More details about that is available in a previous article - Manage Tesla climate control system using PowerShell.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK