Set presence for an end user in Microsoft Teams!

I recently discovered that you now can set presence for an end user using Application rights. Earlier you only could do this with Delegated access.

You need an Azure App Registration with the Presence.ReadWrite.All Application right. You can read more about the presence endpoint here. The following combinations of availability and activity is supported in the requests.

AvailabilityActivityDescription
AvailableAvailableSet the user preferred presence as Available.
BusyBusySet the user preferred presence as Busy.
DoNotDisturbDoNotDisturbSet the user preferred presence as DoNotDisturb.
BeRightBackBeRightBackSet the user preferred presence as BeRightBack.
AwayAwaySet the user preferred presence as Away.
OfflineOffWorkSet the user preferred presence as Offline.

I have created a short script that let you set presence for multiple users. Since we are inputting the users UPN, we need the User.Read.All application right also to be able to get the users object id.

$tenantId = 'xxxxxxxxxxx'
$ClientID = 'xxxxxxxxxxx'
$ClientSecret = 'xxxxxxxxxxx'
$users = "[email protected]","[email protected]"
#Number of hours the presence should be valid.
$Hours = "8"
function Get-MSGraphAppToken{
<# .SYNOPSIS
Get an app based authentication token required for interacting with Microsoft Graph API
.PARAMETER TenantID
A tenant ID should be provided.
.PARAMETER ClientID
Application ID for an Azure AD application. Uses by default the Microsoft Intune PowerShell application ID.
.PARAMETER ClientSecret
Web application client secret.
.EXAMPLE
# Manually specify username and password to acquire an authentication token:
Get-MSGraphAppToken -TenantID $TenantID -ClientID $ClientID -ClientSecert = $ClientSecret
.NOTES
Author: Jan Ketil Skanke
Contact: @JankeSkanke
Created: 2020-15-03
Updated: 2020-15-03
Version history:
1.0.0 – (2020-03-15) Function created
#>
[CmdletBinding()]
param (
[parameter(Mandatory = $true, HelpMessage = "Your Azure AD Directory ID should be provided")]
[ValidateNotNullOrEmpty()]
[string]$TenantID,
[parameter(Mandatory = $true, HelpMessage = "Application ID for an Azure AD application")]
[ValidateNotNullOrEmpty()]
[string]$ClientID,
[parameter(Mandatory = $true, HelpMessage = "Azure AD Application Client Secret.")]
[ValidateNotNullOrEmpty()]
[string]$ClientSecret
)
Process {
$ErrorActionPreference = "Stop"
# Construct URI
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
# Construct Body
$body = @{
client_id = $clientId
scope = "https://graph.microsoft.com/.default"
client_secret = $clientSecret
grant_type = "client_credentials"
}
try {
$MyTokenRequest = Invoke-WebRequest –Method Post –Uri $uri –ContentType "application/x-www-form-urlencoded" –Body $body –UseBasicParsing
$MyToken =($MyTokenRequest.Content | ConvertFrom-Json).access_token
If(!$MyToken){
Write-Warning "Failed to get Graph API access token!"
Exit 1
}
$MyHeader = @{"Authorization" = "Bearer $MyToken" }
}
catch [System.Exception] {
Write-Warning "Failed to get Access Token, Error message: $($_.Exception.Message)"; break
}
return $MyHeader
}
}
$global:Header = Get-MSGraphAppToken –TenantID $tenantId –ClientID $ClientID –ClientSecret $ClientSecret
foreach($user in $users){
$uri = "https://graph.microsoft.com/v1.0/users/$user"
$UserID = (Invoke-RestMethod –Uri $uri –Method GET –Headers $global:Header -ContentType "application/json").id
$body = @"
{
"availability": "DoNotDisturb",
"activity": "DoNotDisturb",
"expirationDuration": "PT$($Hours)H"
}
"@
$uri = "https://graph.microsoft.com/beta/users/$userid/presence/setUserPreferredPresence"
Invoke-RestMethod –Uri $uri –Method Post –Body $body –Headers $global:Header -ContentType "application/json"
}
view raw SetPresence.ps1 hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Connecting to %s