In search of this same solution, I found what I needed under a different question in stackoverflow: Powershell-log-off-remote-session. The below one line will return a list of logged on users.
How to get PS session info for MS Teams, Exchange Online Management and Azure
When using MS Teams, EXO and Azure via PS I want to script check connection/session status before the script continues.
Connecting to these services is fine e.g. Connect-MicrosoftTeams
Then connection info is displayed e.g.
Account Environment Tenant TenantId
------- ----------- ------------------------------------ ------------------------------------
user@Company portal .com AzureCloud xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
But, how get this info otherwise? As using Get-PSSession shows nothing almost everytime .... by that I mean sometimes I can see a session almost every other time $null result. When there is a PSsession actually showing it usually has a status of 'broken' and yet can successfully run Teams cmdlets without issue.
So how to confirm AzureCloud services (MS Teams, EXO, Azure/AD) connection/session status?
I've tried searching this and not found any results for verifying session status. No mention of it in MS Docs for the Connect-* cmdlets or in the MS Docs Get-PSSession
My workaround has been to prompt to connect and disconnect MS Teams at start and end of script. It makes sense to have the script be able to check connection status. As I would do when scripting PS sessions to on-prem Exchange, AD, etc. previously.
2 answers
Sort by: Most helpful
-
-
Vasil Michev 91,946 Reputation points MVP
2021-10-22T05:58:22.64+00:00 Those modules do not use Remote PowerShell sessions, only the Exchange Online one does, and that's probably going away too. And generally speaking you dont need to worry about this, as they use HTTPS sessions, with tokens being renewed automatically as needed. Anyway, if you want to have some function to check connectivity, run a simple "get" cmdlet and check against what you know is the expected output. For example, Get-AzureADTenantDetail when using the AAD module.