Posted in : Uncategorized By tony.pham Translate with Google ⟶

2 years ago

On this guide, I will show you how to automate recurring Out of office with Powershell. Recommendations are:

  1. Use a service account with a strong password
  2. Disable MFA on service account
  3. Use Task Scheduler that has Exchange Online module installed

Service Account

I would recommend to use a service account to trigger this script from Task Scheduler. To trigger them, the service account would need to bypass authentication, and you can follow this guide on how to bypass it. Make sure to disable MFA, if you have it required!

How to connect to Powershell modules with predefined credentials – Xenit

Automation script

From the earlier mentioned guide on how to bypass authentication, you will begin with writing this line:

  1. Read-Host -assecurestring | convertfrom-securestring | out-file C:\mysecurestring.txt

This is to encrypt your password to a .txt file where powershell will get the credential from.

  1. Now you will predefine your credentials and connect it with the login towards EO.

    $username = “YourUserName”
    $password = Get-content “C:\mysecurestring.txt” | ConvertTo-SecureString

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$credentials = get-credential -credential $cred

Connect-Exchangeonline -credential $credentials

You can base your reply on a html code:

$reply = ”<html><head></head><body><p>Hi,<P>Thanks for your email.<P><p>Our office hours are Monday-Friday 08:00-17:00.</p><p>Your email will only be read and answered during our office hours.</p><p>For urgent matters, please contact your my colleagues </p><p>Best Regards,

HR </p></body></html>”

You will have to get the get-date cmdlet to make a starting time.

$date = get-date
$endDate = $date.addHours(2)

So with these two cmdlets, we will make a starting time immediately when running the script, and the end time (when OOO is getting disabled) is after two hours after execution.

Use this line to setup Out of office and connect the start time and end time:

Set-MailboxAutoReplyConfiguration -Identity ”hr.department@contoso.com ” -AutoReplyState Scheduled -StartTime $date -InternalMessage $reply -ExternalMessage $reply -EndTime $endDate

But from scheduled task, I will trigger it at 2PM and the script will run at 5PM and will look like this instead:

Set-MailboxAutoReplyConfiguration -Identity ”hr.department@contoso.com ” -AutoReplyState Scheduled -StartTime $date.addHours(3) -InternalMessage $reply -ExternalMessage $reply -EndTime $endDate

You can run this script every day, but if you want to make an exception for weekends, you can use the following line:

$Friday = (get-date).DayOfWeek

If($Friday -eq “Friday”)

{

$dateFriday = get-date
$endDateFriday = $dateFriday.AddHours(66)

Set-MailboxAutoReplyConfiguration -Identity ”hr.department@contoso.com ” -AutoReplyState Scheduled -StartTime $date.addHours(3) -InternalMessage $reply -ExternalMessage $reply -EndTime $endDate

 

}

And use else statement for the weekday ones.

Now you will be able to have auto-reply from 17pm – 08am!

Tags : #Script, auto-reply, Automation, Exchange, Exchange Online, out of office, PowerShell, recurring

Personlig rådgivning

Vi erbjuder personlig rådgivning med författaren för 1400 SEK per timme. Anmäl ditt intresse i här så återkommer vi så snart vi kan.

Add comment

Your comment will be revised by the site if needed.