Install the module:

 

Install-Module -Name MSAL.PS

First get the access token:

#Provide your Office 365 Tenant Domain Name or Tenant Id

$TenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

#Used the Microsoft Graph PowerShell app id. You can create and use your own Azure AD App id if needed.
$AppClientId="14d82eec-204b-4c2f-b7e8-xxxxxxxx" 

$MsalParams = @{
ClientId = $AppClientId
TenantId = $TenantId 
Scopes = "https://graph.microsoft.com/Presence.Read.All" 
}

$MsalResponse = Get-MsalToken @MsalParams
$AccessToken = $MsalResponse.AccessToken

Copy the token and use it for later use, which looks like eyJ0eXAiOxxxxxxxxxxxxxxxx

 

$AccessToken="eyJ0eXAiOxxxxxxxxxxxxxxxx"

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

#Provide the required user's ObjectId to check the presence status, which can be find in the Azure AD
$UserId = "2856880d-8a22-4f72-b05e-810879dfd7fc"

#This request gets the online presence status of the given user
$ApiUrl = "https://graph.microsoft.com/v1.0/users/$UserId/presence"

#Get the presence status
$Response = Invoke-RestMethod -Method GET -Uri $ApiUrl -ContentType "application\json" -Headers $headers
if($Response -ne $null){
write-host "THe status is: " $Response.availability
Exit 0
}

 

This script can also be used to check if Teams service is available

 

Reference

https://developer.microsoft.com/en-us/graph/graph-explorer