Forum Discussion

Riccardo_Trocca's avatar
Riccardo_Trocca
Copper Contributor
May 20, 2021
Solved

Bug - PowerShell Teams Module 2.3.1 - Private SfBPowerShellSessionViaTeamsModule is never closed

I think I've found a bug in the PowerShell TeamsModule 2.3.1

 

The repro steps are quite straightforward:

 

- open 3 PowerShell instances
- call Connect-Teams in each of them
- Call Get-CSOnlineUser in the 1st, 2nd, and 3rd instance
- Call Disconnect-Teams in each of them

 

- open a 4th PowerShell instance
- Call connect-teams
- Call Get-CSOnlineUser
- Error: Get-CsOnlineSession : Connecting to remote server api.interfaces.records.teams.microsoft.com failed with the following error message : The
WS-Management service cannot process the request. The maximum number of concurrent shells for this user has been exceeded. Close existing
shells or raise the quota for this user. For more information, see the about_Remote_Troubleshooting Help topic.

 

Looking at the source code of the PS modules and also decompiling the private assemblies I noticed that the variuos SfbO cmdlet rely on TrpsSessionPsCmds to retrieve a Session object.
Therefore I've found a workaround that proves the bug:

 

after calling DisconnectTeams in the 1st shell, call:

 

$session = Get-PSSession

 

Check that there is a PSSession that is still alive:
$session | Format-List

 

ComputerType : RemoteMachine
ComputerName : api.interfaces.records.teams.microsoft.com
ContainerId :
VMName :
VMId :
ConfigurationName : Microsoft.PowerShell
InstanceId : 163877d5-81e6-464c-b88e-343609f88e70
Id : 3
Name : SfBPowerShellSessionViaTeamsModule_baba9e35-22d3-425b-b575-657090b56145
Availability : Available
ApplicationPrivateData : {SessionInfo, PSVersionTable}
Runspace : System.Management.Automation.RemoteRunspace
State : Opened
IdleTimeout : 900000
OutputBufferingMode : None
DisconnectedOn :
ExpiresOn :

 

You can repeat the same step in shell 2 and 3 and see that you will get similar results.

 

More proof that those sessions should have been deleted:
In one of the first 3 shells call:
Remove-PSSession -session $session

 

Now go to the 4th shell and run Get-CSOnlineUser again and you will see it working.

 

The Remove-PSSession should be automatically invoked by Disconnect-MicrosoftTeams otherwise the PSSession will be pending for a while preventing other sessions to be opened.

  • Pavlo770's avatar
    Pavlo770
    Copper Contributor

    Hi Riccardo_Trocca 
    We faced exactly the same issue with PowerShell Teams Module 2.3.1 and Azure Function that you described. Did you found out a workaround for this issue? As function execution for different tenants gives error.

    Tried manually to remove session by calling Remove-PSSession but this didn't help. Maybe we are doing something wrong

Resources