Teams Powershell and Active Connections

%3CLINGO-SUB%20id%3D%22lingo-sub-2662057%22%20slang%3D%22en-US%22%3ETeams%20Powershell%20and%20Active%20Connections%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2662057%22%20slang%3D%22en-US%22%3E%3CP%3EWith%20the%20shift%20in%20recent%20PowerShell%20modules%20after%20v2%2C%20and%20merging%20all%20the%20SfB%20commands%20into%20it%2C%20it%20has%20changed%20the%20way%20you%20connect.%26nbsp%3B%20However%2C%20because%20PowerShell%20Remoting%20is%20a%20fickle%20beast%2C%20especially%20when%20it%20comes%20to%20using%20a%20cloud%20hosted%20services%20such%20as%20Teams%2C%20remote%20sessions%20sometimes%20just%20disappear%2C%20time%20out%2C%20or%20break.%26nbsp%3B%20I%20previously%20handled%20this%20in%20scripts%20using%20Get-PSSession%20and%20looking%20at%20the%20state%2C%20if%20it%20was%20broken%2C%20I'd%20force%20a%20reconnect.%26nbsp%3B%20So%20my%20scripts%20looked%20something%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20Connect-MSTeams%20%7B%0A%20if%20(%24ForceReconnect%20-or%20(%40(Get-PSSession%20-Name%20'SfBPower*').Count%20-eq%200)%20-or%20(%40(Get-PSSession%20-Name%20'SfBPower*').State%20-contains%20'Broken'))%20%7B%0A%20%20Get-Module%20tmp*%20%7C%20Where-Object%7B%20%24_.Description%20-like%20'*OcsPowershellOAuth*'%20%7D%20%7C%20Remove-Module%0A%20%20Get-PSSession%20-Name%20'SfBPowerShell*'%7C%20Remove-PSSession%0A%20%20%24cs%20%3D%20New-CsOnlineSession%0A%20%20Import-PSSession%20-Session%20%24cs%20-AllowClobber%0A%20%7D%0A%7D%0A%0AConnect-MSTeams%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20worked%20great%20until%20v2%2C%20and%20now%20I%20don't%20see%20any%20sessions%20when%20execute%20Get-PSSession%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22jangliss_0-1629311857153.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F304195iC51896CEB278D12A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22jangliss_0-1629311857153.png%22%20alt%3D%22jangliss_0-1629311857153.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20Teams%20PowerShell%20sessions%20still%20barf%20and%20die%20randomly%2C%20how%20does%20everybody%20else%20handle%20this%3F%20There%20is%20no%20handy%20function%20like%20Get-MicrosoftTeamsSession%20or%20easy%20way%20to%20check%20the%20health%20of%20a%20session%2C%20meaning%20you%20have%20to%20wrap%20every%20command%20in%20try%2Fcatch%20with%20specific%20catches%20for%20session%20issues.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2662057%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdministrator%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2662676%22%20slang%3D%22en-US%22%3ERe%3A%20Teams%20Powershell%20and%20Active%20Connections%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2662676%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F342739%22%20target%3D%22_blank%22%3E%40jangliss%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%20how%20are%20you%3F%20you%20are%20right%20this%20changed%20recently.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20noticed%20that%2C%20Get-PSSession%20will%20return%20something%20only%20when%20you%20call%20-Cs*%20cmdlets%2C%20so%20if%20you%20run%20Get-Team%20you%20will%20not%20have%20anything%20but%20if%20you%20call%2C%20for%20example%2C%26nbsp%3B%3C!--%20StartFragment%20%20--%3E%3CSPAN%3EGet-CsCallingLineIdentity%20you%20will%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AndresGorzelany_0-1629320379222.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F304214iE6E543107F5E8CD8%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22AndresGorzelany_0-1629320379222.png%22%20alt%3D%22AndresGorzelany_0-1629320379222.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C!--%20EndFragment%20%20--%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

With the shift in recent PowerShell modules after v2, and merging all the SfB commands into it, it has changed the way you connect.  However, because PowerShell Remoting is a fickle beast, especially when it comes to using a cloud hosted services such as Teams, remote sessions sometimes just disappear, time out, or break.  I previously handled this in scripts using Get-PSSession and looking at the state, if it was broken, I'd force a reconnect.  So my scripts looked something like this:

 

function Connect-MSTeams {
	if ($ForceReconnect -or (@(Get-PSSession -Name 'SfBPower*').Count -eq 0) -or (@(Get-PSSession -Name 'SfBPower*').State -contains 'Broken')) {
		Get-Module tmp* | Where-Object{ $_.Description -like '*OcsPowershellOAuth*' } | Remove-Module
		Get-PSSession -Name 'SfBPowerShell*'| Remove-PSSession
		$cs = New-CsOnlineSession
		Import-PSSession -Session $cs -AllowClobber
	}
}

Connect-MSTeams

 

This worked great until v2, and now I don't see any sessions when execute Get-PSSession

 

jangliss_0-1629311857153.png

 

As Teams PowerShell sessions still barf and die randomly, how does everybody else handle this? There is no handy function like Get-MicrosoftTeamsSession or easy way to check the health of a session, meaning you have to wrap every command in try/catch with specific catches for session issues.

 

2 Replies

@jangliss 

Hello how are you? you are right this changed recently.

 

I noticed that, Get-PSSession will return something only when you call -Cs* cmdlets, so if you run Get-Team you will not have anything but if you call, for example, Get-CsCallingLineIdentity you will:

 

AndresGorzelany_0-1629320379222.png

 

 

Said that, reconnection should automatically work with latest versions (if you connected interactively), did you experience reconnection issues?