Exchange 2010 Out of Office Fun with Set-MailboxAutoReplyConfiguration…

by Andy Grogan on January 29, 2012 · 2 comments

in Exchange 2010 (Admin), Exchange 2010 (Mailbox), Mailbox Databases, OOOCTA, Out of Office, Powershell

In between some of the other stuff that I have been working on; I have also been thinking about the future of the OOOCTA tool. Currently OOOCTA supports both Exchange 2007 SP 1 (and above) as well as Exchange 2010, however the underling code infrastructure is a little complicated to say the least in order to accomplish the dual support.

As some of you may know, Exchange 2010 supports setting a users OOF natively via the Set-MailboxAutoReplyConfiguration cmdlet, however Exchange 2007 does not have a corresponding command – therefore the OOOCTA contains quite a lot of code logic which makes use of the EWS API to allow for Out of Office functionality to be set on Exchange 2007 mailboxes.

My current line of thinking is to make version 3.5.2 of the OOOCTA tool the last which supports Exchange 2007, and version 4 will be a pure Exchange 2010 product. Taking this route would allow for me to remove a lot of “code bloat”, as well as improve some security and reporting features – as well as make the overall installation experience much simpler.

I have already knocked up a very early version of the “outofofficeservice.exe” which uses the Set-MailboxAutoReplyConfiguration cmdlet and it works like a charm (and reduced the source code within the service executable by around 120 lines).

I have not made up my mind at the moment, and if you are using the (OOOCTA) tool with Exchange 2007 and would like me to continue with the development – then there might be a case for split versions (one for E27 and one for E210) – but please let me know!

Set-MailboxAutoReplyConfiguration

Ok so enough musing about the OOOCTA – let’s have some fun with with the Exchange 2010 Set-MailboxAutoReplyConfiguration cmdlet. Just before we get going I would like to just briefly give you an idea as to why it is so much easier than the EWS API approach in Exchange 2007?

Consider the following sample code (which is strongly based upon a script by Mike Pfeiffer) – which can be used with both Exchange 2007 and Exchange 2010 servers with the EWS 1.2 API installed (note that it is only a code sample ~ if you would like to see the complete script have a look here).

In essence the script uses the EWS 1.2 to create a function called “Set-EWSOOFSettings” that will allow for the OOF to be set on a users mailbox (either in Exchange 2007 or Exchange 2010). Its worth also noting that in order for it to work, the administrator account who calls it needs to have Full Access Permissions on the target mailbox:

function Set-EWSOofSettings() {
        [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)]
        [System.String]
        $Identity,
        [Parameter(Position=1, Mandatory=$false)]
        [System.String]
        $State,
        [Parameter(Position=2, Mandatory=$false)]
        [System.String]
        $ExternalAudience,
        [Parameter(Position=3, Mandatory=$false)]
        [System.DateTime]
        $StartTime,
        [Parameter(Position=4, Mandatory=$false)]
        [System.DateTime]
        $EndTime,
        [Parameter(Position=5, Mandatory=$false)]
        [System.String]
        $InternalReply,
        [Parameter(Position=6, Mandatory=$false)]
        [System.String]
        $ExternalReply,
        [Parameter(Position=7, Mandatory=$false)]
        [System.String]
        $ver = "Exchange2007_SP1"
        )

    begin {
        Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
        
    }

    process {
        $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -arg $ver
        $service.AutodiscoverUrl("administrator@seg.com")    

        if($Identity -notmatch "@") {
            $Identity = (Get-Recipient $Identity).PrimarySMTPAddress.ToString()
        }        

        $oof = $service.GetUserOofSettings($Identity)

        if($StartTime -and $EndTime) {
            $Duration = New-Object Microsoft.Exchange.WebServices.Data.TimeWindow `
            -arg $StartTime,$EndTime
            $PSBoundParameters.Duration = $Duration
            $PSBoundParameters.State = "Scheduled"
            [Void]$PSBoundParameters.remove("StartTime")
            [Void]$PSBoundParameters.remove("EndTime")
        }

        foreach($p in $PSBoundParameters.GetEnumerator()) {
            if($p.key -ne "Identity") {
                $oof."$($p.key)" = $p.value
            }
        }
        $service.SetUserOofSettings($Identity,$oof)
    }
}

Now, compare the above to the “Set-MailboxAutoReplyConfiguration” cmdlet which performs pretty much the same function as the code above:

Set-MailboxAutoReplyConfiguration -id Alan.Reid -InternalMessage "Blah" -ExternalMessage "Blah"

As you can see there is a massive difference, plus the command will execute against target Mailboxes as the account that you are logged on with (which would be your Exchange Administration Account) even if it is not the Mailbox owner (e.g. does not have Full Access Permissions).

Expanding on the “Set-MailboxAutoReplyConfiguration” the following is a sample script that can be executed from the Exchange Management Shell which can take the “Internal” and “External” Out of Office settings for a user from a text file:

# Exchange 2010 - Set Out of Office Script using a text file
# Version 1.0
# Author: Andy Grogan
# http://www.telnetport25.com

Param(
    [Parameter(Mandatory=$true)]
    [System.Management.Automation.ValidateNotNullOrEmptyAttribute()]
    [string] $mbxID,[Parameter(Mandatory=$false)]
    [System.Management.Automation.ValidateNotNullOrEmptyAttribute()]
    [string] $SourceFile,[System.DateTime] $startDate,[Parameter(Mandatory=$false)]
    [System.DateTime] $EndDate
)

$Message = Get-Content $SourceFile

if($startDate -eq $null -or $EndDate -eq $null){
    Set-MailboxAutoReplyConfiguration -Id $mbxID -AutoReplyState Enabled -InternalMessage "$Message" -ExternalMessage "$Message"
}else{
    Set-MailboxAutoReplyConfiguration -Id $mbxID -AutoReplyState Scheduled -InternalMessage "$Message" -ExternalMessage "$Message" -StartTime "$startDate" -EndTime "$endDate"
}

The OOF message is contained within the path that is passed within the “$SourceFile” parameter – therefore in my example the text file contains:

This user has left
Thank you for your e-mail, this user has now left our organisation, please contact McLovin' on xxx22-555.
Regards,
HR - My Organisation

The following screen shots shows the script in action when setting the OOF for the user without a schedule:

.\SetOOFExchange2010.ps1 –mbxID <samAccountName> –SourceFile <path to source file>

oofFunimgs001

The Out of Office for the user will then look like the following:

oofFunimgs002

You can also add the –StartTime and –EndTime parameters to the script to schedule to OOF.

Some further fun can be had by adding in a few HTML tags to the source text file – for example if you change the source file to look like the following:

<Font color="red"><h1>This user has left</h1></font>
Thank you for your e-mail, <b>this user has now left our organisation</b>, please contact McLovin' on xxx22-555.
Regards,
HR - My Organisation

The OOF Messages will look like the following:

oofFunimgs003

Get-MailboxAutoReplyConfiguration

As you might expect, as well as giving you the ability to Set the OOF state in Exchange 2010 you can also get the Out of Office information for a given mailbox using the Get-MailboxAutoReplyConfiguration cmdlet.

The syntax for this command (in its most basic form) is:

Get-MailboxAutoReplyConfiguration –id <samAccountName>

The sample script below gives you a very simple overview of how you can report on the settings of each mailbox in your organisation where the Out of Office status is either “Enabled” or “Scheduled” to a text file:

# Exchange 2010 - Get Out of Office Report to a text file
# Version 1.0
# Author: Andy Grogan
# http://www.telnetport25.com

Param(
    [Parameter(Mandatory=$true)]
    [System.Management.Automation.ValidateNotNullOrEmptyAttribute()]
    [string] $reportFile
)

New-Item -ItemType file $reportFile -Force
$date = Get-Date
Add-Content $reportFile "Out of office report file: $Date"

$repMBXData = Get-Mailbox | Get-MailboxAutoReplyConfiguration | where {$_.AutoReplyState -eq 'Enabled' -or $_.AutoReplyState -eq 'Scheduled'}

foreach($oofEntry in $repMBXData){
        
        Add-Content $reportFile "---------------------------------------------"
        Add-Content $reportFile "Mailbox ID:"
        Add-Content $reportFile $oofEntry.Identity
        Add-Content $reportFile "Out of Office Status:"
        Add-Content $reportFile $oofEntry.AutoReplyState
        Add-Content $reportFile "Internal Message:"
        Add-Content $reportFile $oofEntry.InternalMessage
        Add-Content $reportFile "External Message:"
        Add-Content $reportFile $oofEntry.ExternalMessage
        Add-Content $reportFile "Start Time:"
        Add-Content $reportFile $oofEntry.StartTime
        Add-Content $reportFile "End Time:"
        Add-Content $reportFile $oofEntry.EndTime
        Add-Content $reportFile "---------------------------------------------"
        Add-Content $reportFile "`n"
}
Invoke-Item $reportFile

The script syntax looks like the following:

.\GetOOFExchange2010.ps1 –reportFile <path to report file>

oofFunimgs004

The output of the script looks like the following:

Out of office report file: 01/29/2012 15:39:24
---------------------------------------------
Mailbox ID:
prepAD.local/ExchangeUsers/Alan.Reid
Out of Office Status:
Enabled
Internal Message:
<html>
<body>
<font color="red">
<h1>This user has left</h1>
</font>Thank you for your e-mail, <b>this user has now left our organisation</b>, please contact McLovin' on xxx22-555. Regards, HR - My Organisation
</body>
</html>

External Message:
<html>
<body>
<font color="red">
<h1>This user has left</h1>
</font>Thank you for your e-mail, <b>this user has now left our organisation</b>, please contact McLovin' on xxx22-555. Regards, HR - My Organisation
</body>
</html>

Start Time:
29/01/2012 15:00:00
End Time:
30/01/2012 15:00:00
---------------------------------------------

Using Set-MailboxAutoReplyConfiguration to disable a user OOF

If you would like to disable to OOF for a given user in your environment you can use the following:

Set-MailboxAutoReplyConfiguration –id <mailbox name> –AutoReplyState Disabled

Script Downloads

If you would like to download the scripts (rather than copy and paste them) for this article, I have made them available here and here.

Social

{ 2 comments… read them below or add one }

Thom McKiernan (@thommck) May 16, 2013 at 11:51 am

Thanks for this.
It’s a bit more than I needed but it gave me the idea of storing the message in a text file which is much easier to format than using just the shell!
I’m using this command

Set-MailboxAutoReplyConfiguration -Identity MAILBOXNAME -AutoReplyState Enabled -InternalMessage $(get-content “C:\Scripts\OOF-message.txt”) -ExternalMessage $(get-content “C:\Scripts\OOF-message.txt”)

Reply

Vince January 6, 2016 at 2:54 am

Can the script get mbxID from a CSV file?

Reply

Leave a Comment

Previous post:

Next post: