How to update Teams Call Queue settings based on available Agents

Hi All,

a Customer ask me recently how they could update some Teams Call Queue settings based on the number of available Agents; for example they wants to change the “maximum calls in the Queue” (OverflowThreshold) on a percentage of available (OptIn) Agents.

Let’s see how!

Prerequisites: Azure Automation setup

We will use (again) the Azure Automation solution to achieve this goal.
Before continue, please setup these steps from my previous article:

How to use Lightweight Bot and Azure Automation to interact with Teams Call Queue

  1. Azure Automation Account creation
  2. Add Teams PowerShell Module
  3. Add Azure Automation Credentials

MSAL.PS Module

After that, you have to add the MSAL.PS Module to your Azure Automation Account.
MSAL.PS is a set of PS cmdlet that help you manage the oAuth process.
You can read more on MSAL.PS here

After the import process is completed, check the Modules list to confirm the presence of the MSAL.PS module


In this script we will use Microsoft Graph PowerShell to get Agents Status Presence.
It’s necessary to register/update permissions for this App, to do so open a PowerShell prompt:

Install Microsoft Graph PowerShell module

Install-Module Microsoft.Graph.Authentication -Scope CurrentUser

Register Microsoft Graph PowerShell

Connect-MgGraph -Scopes "User.Read.All","Presence.Read.All"

You can check if everything is ok going to Azure AD > Entrprise App > Microsoft Graph PowerShell > Permissions, verify if the App has the correct rights.


Now that every prerequisites are in place, you have to create the new Runbook where to store the PowerShell script.
Go to your Azure > <Automation Resource Group> > Automation Account > Runbooks > Create a runbook
Runbook type: PowerShell
Version: 5.1


These are the Script main functions:

  • Get the list of OptedIn Agents into the Teams Call Queue. The script will not check the Presence Status of OptedOut Agents
  • For each Agents OptedIn, it will retrieve the Teams Status Presence
  • Call Queue Agents are counted, grouped by Presence
  • Set Call Queue Overflow Threshold with a predefined rule, in this example it’s “2xAvailableAgents + 3”, but of course you can set whatever you want or need.
# Authenticate to Teams
$Credential = Get-AutomationPSCredential -Name 'TeamsAdmin'
$Session = Connect-MicrosoftTeams -credential $Credential

# Retrieve OptIn Agents from the Queue
$OptInAgents = (Get-CsCallQueue -Identity df6ca335-c04f-4cc1-a105-1fe0abf65f2f).Agents | Where-Object OptIn -eq True

# Graph API Access Token
$MsalParams = @{
    ClientId = $AppClientId
    TenantId = "<ENTER TENANT ID HERE>"  
    Scopes   = ""   

$MsalResponse = Get-MsalToken @MsalParams -UserCredential $Credential -AzureCloudInstance 1
$AccessToken  = $MsalResponse.AccessToken

#Form request headers with the acquired $AccessToken
$headers = @{'Content-Type'="application/json";'Authorization'="Bearer $AccessToken"}

#Get Agent Presence Status
$AgentStatus = @()

Foreach ($Agent in $OptInAgents) 
    #This request gets the online presence status of the given Agent
    $ApiUrl = "$($Agent.ObjectID)/presence"
    #Get the presence status
    $AgentStatus += Invoke-RestMethod -Method GET -Uri $ApiUrl -ContentType "application/json" -Headers $headers

#Cout Agents by Presence Status
$AgentsOptIn = $OptInAgents.Count
$AgentsAvailable = ($AgentStatus.availability -match 'Available').Count
$AgentsAway = ($AgentStatus.availability -match 'Away').Count
$AgentsOffline = ($AgentStatus.availability -match 'Offline').Count
$AgentsBusy = ($AgentStatus.availability -match 'Busy').Count
$AgentsDnD = ($AgentStatus.availability -match 'DoNotDisturb').Count

#Set CQ Overflow Threshold Calc System
$OverflowThreshold = 3+($AgentsAvailable)*2

#Set CQ Overflow Threshold
Set-CsCallQueue -Identity <CallQueueIdentity> -OverflowThreshold $OverflowThreshold

Enter the newly created Runbook and click Edit.
Copy and paste the script, update the required value (TenantID, CallQueueID, Overflow Calc System) and publish it.
Test it and schedule it to run as you prefer.

As always, I hope this could help some of you!
Best Regards.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at

Up ↑

%d bloggers like this: