Forum Discussion
Bridget_T
Nov 12, 2024Brass Contributor
script to use PowerShell to Report Teams Status, Presence and Reset After Time
I would like to create a powershell script to run against my tenant to report where people have got a Reset Status After time coded in Teams Any suggestions - greatfully received. I can only fin...
- Nov 18, 2024
Hi Bridget_T ,
This is more of a Teams question than a PowerShell question, so you might want to also ask this in a Teams forum.
Having had a read of the v1 and beta Graph presence endpoint, I does not seem that what you are wanting to do is possible. This is for two reasons:
- Teams can have multiple clients connected to it, where each client is responsible for managing its own polling and setting of the per session status;
- The "set presence" endpoint does not support a value for "reset status", adding weight to the above point that this is not managed service-side, but rather within each client's session.
Because the "reset" behaviour is being actioned by each client, that means there is nothing to query from the service itself, which is reflected in the "get presence" endpoint where the JSON response contains no such data.
Put another way: Setting a reset value within the Teams desktop client means its then that desktop client's responsibility to perform the reset behaviour. If you concurrently ran Teams mobile and Teams web, they would be unaware of the reset value from Teams desktop. Similarly, there is nothing to query from the Teams service since it's the Teams desktop client holding these settings and actioning the behaviour.
Reference articles:
- presence: setPresence - Microsoft Graph beta | Microsoft Learn
- Get presence - Microsoft Graph beta | Microsoft Learn
As I mentioned earlier though, it would be worth your while asking in a Teams forum, where you might expect to find more Teams-proficient people. Another option might be to ask in a Power Platform forum, as Power Platform solutions are a prolific consumer of Graph endpoints.
In not being a Teams afficionado, and going off the Graph endpoint documentation, I can't see a way to achieve what you're hoping to achieve.
Cheers,
Lain
kyazaferr
Nov 13, 2024Steel Contributor
# Ensure Teams PowerShell Module is installed
Install-Module -Name MicrosoftTeams -Force -AllowClobber
Import-Module MicrosoftTeams
# Connect to Microsoft Teams
$credential = Get-Credential
Connect-MicrosoftTeams -Credential $credential
# Function to get presence status for all users in the tenant
Function Get-TeamsPresence {
$users = Get-TeamUserPresence
$results = @()
foreach ($user in $users) {
# Output user presence status and the time they were last active
$userStatus = New-Object PSObject -property @{
UserName = $user.User
Presence = $user.Presence
LastActivityTime = $user.LastActivityTime
}
$results += $userStatus
}
return $results
}
# Function to report presence
Function Report-UserPresence {
$results = Get-TeamsPresence
$currentTime = Get-Date
foreach ($user in $results) {
$timeDiff = $currentTime - $user.LastActivityTime
if ($timeDiff.TotalMinutes -gt 60) { # If more than 60 minutes idle
Write-Host "User $($user.UserName) has been idle for $([math]::Round($timeDiff.TotalMinutes, 2)) minutes."
# Action to reset presence could be added here with Microsoft Graph API if available
# Example: Update presence using Microsoft Graph API (if such an endpoint is available)
# ResetTeamsPresence($user.UserName)
} else {
Write-Host "$($user.UserName) - Current Status: $($user.Presence) - Last Active: $($user.LastActivityTime)"
}
}
}
# Call function to report status and reset after time
Report-UserPresence
# To automate resetting the status after a specific time
# You would need to use Microsoft Graph API (Presence API) or set up a flow in Power Automate if needed