question

IanL-5055 avatar image
0 Votes"
IanL-5055 asked KyleXu-MSFT edited

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@contoso.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

imamitsingh avatar image
1 Vote"
imamitsingh answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

michev avatar image
0 Votes"
michev answered IanL-5055 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your reply.

Its good to know a bit more about what is going on ie. it is a HTTPS session and token.

My question becomes then why omit session information if it serves no purpose, if it cannot be checked or validated like what can be done with PS sessions.

I was taught a best practice scripting will check if there is a open session and if not open the session once the script has completed processing to close the session.
e.g.
New user account creation

  1. Checks if there is an open PS session to domain controller to create AD User account if not opens one

  2. Check if there is an open PS session to Exchange if not opens one

  3. Script runs creating account, applies group memberships etc.

  4. Script runs creating mailbox etc.

  5. Closes sessions at end of script.

Having to script a pretend cmlet with error handling to confirm if a session to Teams or EXO does not make sense.



0 Votes 0 ·