The intent of this post is to show how PowerShell (and the Powershell Graph cmdlets) can be leveraged to poll a user (or group of users) current status in Teams.

Assumptions:

  1. You have installed the Microsoft Graph Powershell module
  2. You have consented to the proper Graph API permissions when running the module (if not you will have an opportunity to do so on the first run)

Connect to Microsoft Graph via Powershell using your account credentials

  • Open Powershell and connect to the Teams Graph API
    • Connect-MgGraph -Scopes Presence.Read.All,User.Read.All
  • You will need to login using your browser and then consent to the API access
  • Once completed you should see the Welcome message
  • Confirm the connection by getting your user account

Query the current Teams status

  • Use the Id that you captured above to query your current Teams status
    • Get-MgCommunicationPresence -PresenceId $UserId.Id | select Activity, Availability
  • You should see your current Teams status
  • Verify that the status is correct by changing your Teams status and re-running the command above

Now that we have the core function we can begin to build around it to create something that will pull the status of all users in a settings file and perform an action on them

Query status of a list of users from a CSV

  • Create a simple CSV file with the follwing in it:
Username,Enabled
[email protected],y
  • Import the list of users from a CSV and get the last written time on the file so we know when it was last changed
#Enter the path to your CSV file
$csvPath = Get-ChildItem {The Path to your CSV file}

#This will get the last time the file was updated, we will use it later to tell the script when to reload the file
$lastWriteTime = $csvPath.LastWriteTime

#This will populate the list of users
$graphUsers = Import-Csv $csvPath.FullName

Build a simple loop to iterate through each user in the CSV

#Run a simple loop on each user in the CSV that is enabled (y), get their Id and then their status

#Initialize the array
$allUserStatus = @()


foreach ($graphUser in $graphUsers | Where-Object {$_.enabled -like 'y'})
{
    #Get the Unique GUID for each UPN provided in the CSV, this is to make things easier to manage as you just need to enter the email address
    $UserId = Get-MgUser -UserId $graphUser.username

    #Capture the current Teams presence
    $Presence = Get-MgCommunicationPresence -PresenceId $UserId.Id

    #Create an array for each user so you can link the user email address with their current status and GUID
    $arrayProperties = [ordered] @{
        DisplayName = $UserId.DisplayName
        Activity = $Presence.Activity
        Availability = $Presence.Availability
        GUID = $Presence.Id
     }

    #Combine each user int the array so you can perform an action based on any of the users Teams status
    $outPut = New-Object PSObject -Property $arrayProperties
    $allUserStatus += $outPut 

}

Now perform an action if any of the users that you queried are in a specific state

#Identify if any of the users are in a call or set to do not disturb
If (($allUserStatus.Activity -contains "InACall") -or ($allUserStatus.Availability -contains "DoNotDisturb"))
{
    Write-Host "Someone is busy right now!" -ForegroundColor Red
}

#Identify if nobody is busy at the moment and write it to the console
If (($allUserStatus.Activity -notcontains "InACall") -and ($allUserStatus.Availability -notcontains "DoNotDisturb"))
{
    Write-Host "Nobody is busy right now" -ForegroundColor Green

}

Wrap the action chunk in a Do/While loop in order to keep it running to show the change every few seconds

#Check to see if the CSV file has been updated, if you want to add/remove someone from the list just switch the y to an N and save the csv, it will automatically be loaded up.
#Additionally, if you want to add more people to the list, just put their name and Y in the list.

Do {

    $csvPath = Get-ChildItem $Path
    If ($csvPath.LastWriteTime -gt $lastWriteTime)
    {
        Write-Host "Reloading the user list..." -ForegroundColor Yellow
        $csvPath = Get-ChildItem $Path
        $lastWriteTime = $csvPath.LastWriteTime
        $graphUsers = Import-Csv $csvPath.FullName   
    }

#Main Logic here:

while ($null -eq $stop) 

Now put it all together as seen below:

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

#Enter the path to your CSV file
$csvPath = Get-ChildItem {The Path to your CSV file}

#This will get the last time the file was updated, we will use it later to tell the script when to reload the file
$lastWriteTime = $csvPath.LastWriteTime

#This will populate the list of users
$graphUsers = Import-Csv $csvPath.FullName

$Path = "The Path to your CSV"

$csvPath = Get-ChildItem $Path
$lastWriteTime = $csvPath.LastWriteTime
$graphUsers = Import-Csv $csvPath.FullName

#Check to see if the CSV file has been updated, if you want to add/remove someone from the list just switch the y to an N and save the csv, it will automatically be loaded up.
#Additionally, if you want to add more people to the list, just put their name and Y in the list.

Do {

    $csvPath = Get-ChildItem $Path
    If ($csvPath.LastWriteTime -gt $lastWriteTime)
    {
        Write-Host "Reloading the user list..." -ForegroundColor Yellow
        $csvPath = Get-ChildItem $Path
        $lastWriteTime = $csvPath.LastWriteTime
        $graphUsers = Import-Csv $csvPath.FullName   
    }


#Check the user status every 5 seconds
Start-Sleep -Seconds 5

#Initialize the array
$allUserStatus = @()

#Run a simple loop on each user in the CSV that is enabled (y), get their Id and then their status

foreach ($graphUser in $graphUsers | Where-Object {$_.enabled -like 'y'})
{
    #Get the Unique GUID for each UPN provided in the CSV, this is to make things easier to manage as you just need to enter the email address
    $UserId = Get-MgUser -UserId $graphUser.username

    #Capture the current Teams presence
    $Presence = Get-MgCommunicationPresence -PresenceId $UserId.Id

    #Create an array for each user so you can link the user email address with their current status and GUID
    $arrayProperties = [ordered] @{
        DisplayName = $UserId.DisplayName
        Activity = $Presence.Activity
        Availability = $Presence.Availability
        GUID = $Presence.Id
     }

    #Combine each user int the array so you can perform an action based on any of the users Teams status
    $outPut = New-Object PSObject -Property $arrayProperties
    $allUserStatus += $outPut 

}


#Identify if any of the users are in a call or set to do not disturb
If (($allUserStatus.Activity -contains "InACall") -or ($allUserStatus.Availability -contains "DoNotDisturb"))
{
    Write-Host "Someone is busy right now!" -ForegroundColor Red
}

#Identify if nobody is busy at the moment and write it to the console
If (($allUserStatus.Activity -notcontains "InACall") -and ($allUserStatus.Availability -notcontains "DoNotDisturb"))
{
    Write-Host "Nobody is busy right now" -ForegroundColor Green

}

}
while ($null -eq $stop) 

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *