Skip to content

Instantly share code, notes, and snippets.

@jdmills-edu
Last active September 20, 2017 20:23
Show Gist options
  • Save jdmills-edu/a0968a813d83bc580415caa5b6776920 to your computer and use it in GitHub Desktop.
Save jdmills-edu/a0968a813d83bc580415caa5b6776920 to your computer and use it in GitHub Desktop.
This PowerShell script synchronizes the Out-Of-Office status of your Zendesk agent Exchange mailboxes with the Zendesk Out-Of-Office Extension. This provides a much better user experience to your agents than requiring them to set their statuses separately in Outlook and Zendesk. Run this as a scheduled task.
#Download all admin and agent user objects from Zendesk.
$agents = &ZendeskAPI-GetZendeskUsers.ps1 "allagents"
#Trim agent list.
$agents = $agents | where email -like "*@yourdomain.com" | sort email
#Connect to Exchange Online. Use your own script here or swap this line out with the connection commands supplied by Microsoft.
#There's no reason this won't work with Exchange On-Prem AFAIK. We just use Exchange Online, so the comments reference it.
&ConnectToExchangeOnline.ps1
#New array to hold agent email, last Out of Office status, current Out of Office status.
$oooStatuses = @()
$i = 0
$agents | ForEach-Object {
#Get last OoO status from Zendesk agent user fields.
$lastOoOStatus = $_.user_fields.agent_ooo
#Get current OoO status from Exchange Online.
$currentOoOStatus = $_.email | Get-MailboxAutoReplyConfiguration | select AutoReplyState, StartTime, EndTime
#Create a custom object to hold all of that.
$oooStatus = [PSCustomObject]@{
email = $_.email
lastOoOStatus = $lastOoOStatus.ToString()
currentOoOStatus = $currentOoOStatus.AutoReplyState
startTime = $currentOoOStatus.StartTime
endTime = $currentOoOStatus.EndTime
}
#Add the custom object to the array.
$oooStatuses += $oooStatus
#Show a progress bar.
Write-Progress -Activity "Getting Out of Office status from Exchange Online..." -PercentComplete (($i/$agents.Count)*100) -Status $_.email
$i++
}
#Close the progress bar.
Write-Progress -Activity "Getting Out of Office status from Exchange Online..." -Completed
#Zendesk API Connection Headers Referencing System Environmental Variables for username and API token.
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($env:ZendeskAPI_Username):$($env:ZendeskAPI_Token)"));}
#Zendesk API: create_or_update
$seturi = "https://yourdomain.zendesk.com/api/v2/users/create_or_update.json"
#Look for changes in Out of Office status, do stuff if changes are found.
$oooStatuses | ForEach-Object {
#Convert Exchange Online OoO status into a true or false.
if($_.currentOoOStatus -eq "Enabled"){
$isooo = "true"
}
elseif(($_.currentOoOStatus -eq "Scheduled") -and ($_.startTime -lt (Get-Date) -and $_.endTime -gt (Get-Date))){
$isooo = "true"
}
else {
$isooo = "false"
}
#Compare last OoO satus and current OoO status.
if(($_.lastOoOStatus -ne $isooo) -and ($isooo -eq "true")){
Write-Host $_.email "enabled their Out of Office auto-reply." -ForegroundColor Cyan
}
if(($_.lastOoOStatus -ne $isooo) -and ($isooo -eq "false")){
Write-Host $_.email "disabled their Out of Office auto-reply." -ForegroundColor Magenta
}
#If a change in OoO status is detected, update Zendesk.
if($_.lastOoOStatus -ne $isooo){
#Build the JSON.
$setjson = '{"user": {
"email": "'+$_.email+'",
"user_fields": {
"agent_ooo": "'+$isooo+'"
}
}
}'
#Echo the current custom object and the JSON.
$_
$setjson
#Post the JSON body to the Zendesk API
Invoke-RestMethod -Uri $seturi -Method Post -Headers $headers -ContentType "application/json" -Body $setjson
}
}
@jdmills-edu
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment