Microsoft Tech Community Live:  Microsoft Teams Edition
November 09, 2021, 08:00 AM - 12:00 PM (PST)
SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-2372508%22%20slang%3D%22en-US%22%3EBug%20-%20PowerShell%20Teams%20Module%202.3.1%20-%20Private%20SfBPowerShellSessionViaTeamsModule%20is%20never%20closed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2372508%22%20slang%3D%22en-US%22%3E%3CP%3EI%20think%20I've%20found%20a%20bug%20in%20the%20PowerShell%20TeamsModule%202.3.1%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20repro%20steps%20are%20quite%20straightforward%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E-%20open%203%20PowerShell%20instances%3CBR%20%2F%3E-%20call%20Connect-Teams%20in%20each%20of%20them%3CBR%20%2F%3E-%20Call%20Get-CSOnlineUser%20in%20the%201st%2C%202nd%2C%20and%203rd%20instance%3CBR%20%2F%3E-%20Call%20Disconnect-Teams%20in%20each%20of%20them%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E-%20open%20a%204th%20PowerShell%20instance%3CBR%20%2F%3E-%20Call%20connect-teams%3CBR%20%2F%3E-%20Call%20Get-CSOnlineUser%3CBR%20%2F%3E-%20Error%3A%20Get-CsOnlineSession%20%3A%20Connecting%20to%20remote%20server%20api.interfaces.records.teams.microsoft.com%20failed%20with%20the%20following%20error%20message%20%3A%20The%3CBR%20%2F%3EWS-Management%20service%20cannot%20process%20the%20request.%20The%20maximum%20number%20of%20concurrent%20shells%20for%20this%20user%20has%20been%20exceeded.%20Close%20existing%3CBR%20%2F%3Eshells%20or%20raise%20the%20quota%20for%20this%20user.%20For%20more%20information%2C%20see%20the%20about_Remote_Troubleshooting%20Help%20topic.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELooking%20at%20the%20source%20code%20of%20the%20PS%20modules%20and%20also%20decompiling%20the%20private%20assemblies%20I%20noticed%20that%20the%20variuos%20SfbO%20cmdlet%20rely%20on%20TrpsSessionPsCmds%20to%20retrieve%20a%20Session%20object.%3CBR%20%2F%3ETherefore%20I've%20found%20a%20workaround%20that%20proves%20the%20bug%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eafter%20calling%20DisconnectTeams%20in%20the%201st%20shell%2C%20call%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24session%20%3D%20Get-PSSession%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheck%20that%20there%20is%20a%20PSSession%20that%20is%20still%20alive%3A%3CBR%20%2F%3E%24session%20%7C%20Format-List%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EComputerType%20%3A%20RemoteMachine%3CBR%20%2F%3EComputerName%20%3A%20api.interfaces.records.teams.microsoft.com%3CBR%20%2F%3EContainerId%20%3A%3CBR%20%2F%3EVMName%20%3A%3CBR%20%2F%3EVMId%20%3A%3CBR%20%2F%3EConfigurationName%20%3A%20Microsoft.PowerShell%3CBR%20%2F%3EInstanceId%20%3A%20163877d5-81e6-464c-b88e-343609f88e70%3CBR%20%2F%3EId%20%3A%203%3CBR%20%2F%3EName%20%3A%20SfBPowerShellSessionViaTeamsModule_baba9e35-22d3-425b-b575-657090b56145%3CBR%20%2F%3EAvailability%20%3A%20Available%3CBR%20%2F%3EApplicationPrivateData%20%3A%20%7BSessionInfo%2C%20PSVersionTable%7D%3CBR%20%2F%3ERunspace%20%3A%20System.Management.Automation.RemoteRunspace%3CBR%20%2F%3EState%20%3A%20Opened%3CBR%20%2F%3EIdleTimeout%20%3A%20900000%3CBR%20%2F%3EOutputBufferingMode%20%3A%20None%3CBR%20%2F%3EDisconnectedOn%20%3A%3CBR%20%2F%3EExpiresOn%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20repeat%20the%20same%20step%20in%20shell%202%20and%203%20and%20see%20that%20you%20will%20get%20similar%20results.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMore%20proof%20that%20those%20sessions%20should%20have%20been%20deleted%3A%3CBR%20%2F%3EIn%20one%20of%20the%20first%203%20shells%20call%3A%3CBR%20%2F%3ERemove-PSSession%20-session%20%24session%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20go%20to%20the%204th%20shell%20and%20run%20Get-CSOnlineUser%20again%20and%20you%20will%20see%20it%20working.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Remove-PSSession%20should%20be%20automatically%20invoked%20by%20Disconnect-MicrosoftTeams%20otherwise%20the%20PSSession%20will%20be%20pending%20for%20a%20while%20preventing%20other%20sessions%20to%20be%20opened.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2372508%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2372679%22%20slang%3D%22en-US%22%3ERe%3A%20Bug%20-%20PowerShell%20Teams%20Module%202.3.1%20-%20Private%20SfBPowerShellSessionViaTeamsModule%20is%20never%20closed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2372679%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1043755%22%20target%3D%22_blank%22%3E%40Riccardo_Trocca%3C%2FA%3E%26nbsp%3B%20thanks%20for%20taking%20the%20time.%26nbsp%3B%20The%20%3CA%20href%3D%22https%3A%2F%2Fmicrosoftteams.uservoice.com%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EUservoice%20feedback%20forum%3C%2FA%3E%20is%20the%20place%20to%20report%20bugs%20and%20make%20feature%20requests%2C%20and%20then%20other%20users%20can%20vote%20on%20them%20and%20track%20their%20progress.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2579405%22%20slang%3D%22en-US%22%3ERe%3A%20Bug%20-%20PowerShell%20Teams%20Module%202.3.1%20-%20Private%20SfBPowerShellSessionViaTeamsModule%20is%20never%20closed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2579405%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1043755%22%20target%3D%22_blank%22%3E%40Riccardo_Trocca%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3EWe%20faced%20exactly%20the%20same%20issue%20with%26nbsp%3BPowerShell%20Teams%20Module%202.3.1%20and%20Azure%20Function%20that%20you%20described.%20Did%20you%20found%20out%20a%20workaround%20for%20this%20issue%3F%20As%20function%20execution%20for%20different%20tenants%20gives%20error.%3C%2FP%3E%3CP%3ETried%20manually%20to%20remove%20session%20by%20calling%26nbsp%3B%3CSPAN%3ERemove-PSSession%20but%20this%20didn't%20help.%20Maybe%20we%20are%20doing%20something%20wrong%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2579488%22%20slang%3D%22en-US%22%3ERe%3A%20Bug%20-%20PowerShell%20Teams%20Module%202.3.1%20-%20Private%20SfBPowerShellSessionViaTeamsModule%20is%20never%20closed%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2579488%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1110968%22%20target%3D%22_blank%22%3E%40Pavlo770%3C%2FA%3E%2C%26nbsp%3Bwhat%20I%20did%20was%20to%20call%20Remove-PSSession%20in%20the%20way%20I%20describe%20here%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fswissdev.medium.com%2Fmicrosoftteams-powershell-module-2-3-1-and-sfb-cmdlets-631c7f6a9664%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EDangling%20SfBO%20sessions%20with%20Microsoft%20Teams%20Module%202.x%20%7C%20by%20Riccardo%20Trocca%20%7C%20May%2C%202021%20%7C%20Medium%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20noticed%20that%20under%20certain%20conditions%20Get-PSSession%20returns%20null%20and%2C%20of%20course%2C%20in%20that%20case%20we%20are%20stuck.%20We%20had%20to%20redesign%20our%20solution%20to%20work%20around%20this%20issue%2C%20sadly.%20I%20tried%20to%20report%20this%20to%20Microsoft%20but%20they%20told%20me%20that%20this%20is%20%22by%20design%22.%20I%20hope%20they%20will%20fix%20this%20stuff%20soon.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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.

3 Replies
best response confirmed by ThereseSolimeno (Microsoft)
Solution

Hi @Riccardo_Trocca  thanks for taking the time.  The Uservoice feedback forum is the place to report bugs and make feature requests, and then other users can vote on them and track their progress.  

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

Hi @Pavlo770, what I did was to call Remove-PSSession in the way I describe here:
Dangling SfBO sessions with Microsoft Teams Module 2.x | by Riccardo Trocca | May, 2021 | Medium

I noticed that under certain conditions Get-PSSession returns null and, of course, in that case we are stuck. We had to redesign our solution to work around this issue, sadly. I tried to report this to Microsoft but they told me that this is "by design". I hope they will fix this stuff soon.