How to get PS session info for MS Teams, Exchange Online Management and Azure

Ian L 6 Reputation points
2021-10-22T02:25:11.45+00:00

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.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,305 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Amit Singh 4,846 Reputation points
    2021-10-22T05:18:03.38+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. 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.