MicrosoftTeams cmdlets not working with AccessToken

%3CLINGO-SUB%20id%3D%22lingo-sub-2307796%22%20slang%3D%22en-US%22%3EMicrosoftTeams%20cmdlets%20not%20working%20with%20AccessToken%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2307796%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20run%20cmdlets%20from%20powershell%20module%20MicrosoftTeams%20(version%202.0.0)%20in%20a%20C%23%20web%20application.%20I%20am%20using%20Authorization%20code%20flow%20and%20code%20from%20the%20answer%20provided%20in%20this%20post%20to%20acquire%20token%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F43488511%2Facquire-aad-token-using-asp-net-web-forms%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAcquire%20AAD%20token%20using%20ASP.Net%20web%20forms%3C%2FA%3E.%20Note%3A%20I%20had%20changed%20resource%20in%20the%20code%20to%26nbsp%3Bgraph.windows.net%26nbsp%3Bto%20acquire%20AAD%20token.%20Token%20is%20acquired%20by%20using%20AuthenticationContext.AcquireTokenByAuthorizationCodeAsync%20method.%26nbsp%3BOnce%20the%20token%20is%20acquired%2C%20I%20run%20the%20following%20lines%20to%20create%20a%20powershell%20instance%20in%20C%23%20and%20to%20import%20MicrosoftTeams%20Module.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EPowerShell%20pshell%0AInitialSessionState%20iss%3B%0Aiss%20%3D%20InitialSessionState.CreateDefault2()%3B%0Aiss.ImportPSModule(new%5B%5D%20%7B%20%22MicrosoftTeams%22%20%7D)%3B%0Apshell%20%3D%20PowerShell.Create(iss)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20to%20connect%20with%20MicrosoftTeams%2C%20I%20run%20the%20following%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Evar%20connectCmd%20%3D%20new%20Command(%22Connect-MicrosoftTeams%22)%3B%0AconnectCmd.Parameters.Add(%22AadAccessToken%22%2C%20AccessToken)%3B%0AconnectCmd.Parameters.Add(%22AccountId%22%2C%20%22xxxxxxx%40xxxxxx.onmicrosoft.com%22)%3B%0Apshell.Commands.AddCommand(connectCmd)%3B%0Avar%20result1%20%3D%20pshell.Invoke()%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECode%20works%20fine%20till%20here.%3C%2FP%3E%3CP%3EAfter%20this%20I%20clear%20the%20shell%20commands%20and%20invoke%20the%20Get-CsTeamsCallingPolicy%20cmdlet%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Epshell.Commands.Clear()%3B%0Apshell.Streams.Error.Clear()%3B%0A%20%20%20%20%0Apshell.AddScript(%22Get-CsTeamsCallingPolicy%22)%3B%0Avar%20result2%20%3D%20pshell.Invoke()%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20Invoke%2C%20I%20get%20an%20exception%20and%20this%20dialog%20pops%20up%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22getcsinternalaccesstoken.png%22%20style%3D%22width%3A%20790px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F276850i185CA6CEA523ADA5%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22getcsinternalaccesstoken.png%22%20alt%3D%22getcsinternalaccesstoken.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EPressing%20'Continue'%20brings%20back%20the%20same%20dialogue%20a%20couple%20of%20times.%3C%2FP%3E%3CP%3EException%20details%20from%20this%20screen%20are%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESystem.Collections.Generic.KeyNotFoundException%20was%20unhandled%20by%20user%20code%0A%20%20HResult%3D-2146232969%0A%20%20Message%3DThe%20given%20key%20was%20not%20present%20in%20the%20dictionary.%0A%20%20Source%3Dmscorlib%0A%20%20StackTrace%3A%0A%20%20%20%20%20%20%20at%20System.Collections.Concurrent.ConcurrentDictionary%602.get_Item(TKey%20key)%0A%20%20%20%20%20%20%20at%20Microsoft.TeamsCmdlets.Powershell.Connect.Models.AzureSessionProvider.GetAccessToken(String%20resource%2C%20IEnumerable%601%20scopes)%20in%20D%3A%5Ca%5C1%5Cs%5Csrc%5CMicrosoft.TeamsCmdlets.PowerShell.Connect%5CModels%5CAzureSession.cs%3Aline%2080%0A%20%20%20%20%20%20%20at%20Microsoft.TeamsCmdlets.Powershell.Connect.TeamsPowerShellSession.GetAccessToken(String%20resource%2C%20IEnumerable%601%20scopes)%20in%20D%3A%5Ca%5C1%5Cs%5Csrc%5CMicrosoft.TeamsCmdlets.PowerShell.Connect%5CTeamsPowerShellSession.cs%3Aline%2082%0A%20%20%20%20%20%20%20at%20Microsoft.TeamsCmdlets.PowerShell.Connect.GetCsInternalAccessToken.ProcessRecord()%20in%20D%3A%5Ca%5C1%5Cs%5Csrc%5CMicrosoft.TeamsCmdlets.PowerShell.Connect%5CGetCsInternalAccessToken.cs%3Aline%2061%0A%20%20%20%20%20%20%20at%20System.Management.Automation.CommandProcessor.ProcessRecord()%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20pressing%20continue%20for%20the%203rd%20time%2C%20control%20goes%20back%20to%20C%23%20code%2C%20and%20I%20receive%20the%20following%20runtime%20exception%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EException%20calling%20%22GetSteppablePipeline%22%20with%20%221%22%20argument(s)%3A%20%0A%22Exception%20calling%20%22GetRemoteNewCsOnlineSession%22%20with%20%221%22%20argument(s)%3A%20%0A%22Run%20either%20Connect-MicrosoftTeams%20or%20new-csonlinesession%20before%20running%20cmdlets.%22%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETrying%20to%20run%20this%20logic%20from%20the%20powershell%20editor%20shows%20similar%20behavior%3A%3C%2FP%3E%3CP%3ERunning%20the%20following%20two%20lines%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24AccessToken%20%3D%20%20'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'%20%0AConnect-MicrosoftTeams%20-AadAccessToken%20%24AccessToken%20-AccountId%20'xxxxxxx%40xxxxxx.onmicrosoft.com'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Egives%20this%20result%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EAccount%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Environment%20Tenant%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TenantId%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A-------%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-----------%20------%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--------%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0Axxxxxxx%40xxxxxx.onmicrosoft.com%20AzureCloud%20%20xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx%20xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20run%20Get-Team%20cmdlet%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EGet-Team%20-User%20xxxxxxx%40xxxxxxx.onmicrosoft.com%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewhich%20results%20in%20this%20message%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EGet-Team%20%3A%20The%20given%20key%20was%20not%20present%20in%20the%20dictionary.%0A%20At%20line%3A1%20char%3A1%0A%20%2B%20Get-Team%20-User%20xxxxxxx%40xxxxxxx.onmicrosoft.com%0A%20%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20NotSpecified%3A%20(%3A)%20%5BGet-Team%5D%2C%20KeyNotFoundException%0A%20%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20System.Collections.Generic.KeyNotFoundException%2CMicrosoft.TeamsCmdlets.PowerShell.Custom.GetTeam%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERunning%20cmdlet%20Get-CsTeamsCallingPolicy%20yields%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EException%20calling%20%22GetSteppablePipeline%22%20with%20%221%22%20argument(s)%3A%20%22Exception%20calling%20%22GetRemoteNewCsOnlineSession%22%20with%20%221%22%20argument(s)%3A%20%22Run%20either%20Connect-MicrosoftTeams%20or%20new-csonlinesession%20before%20running%20cmdlets.%22%22%0AAt%20C%3A%5CProgram%20Files%5CWindowsPowerShell%5CModules%5CMicrosoftTeams%5C2.0.0%5Cnet472%5CSfBORemotePowershellModule.psm1%3A11369%20char%3A13%0A%2B%20%24steppablePipeline%20%3D%20%24scriptCmd.GetSteppablePipeline(%24myI%20...%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20ParentContainsErrorRecordException%0A%2B%20FullyQualifiedErrorId%20%3A%20CmdletInvocationException%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20run%20Connect-MicrosoftTeams%20directly%20from%20powershell%20without%20providing%20access%20token%20and%20accountid%2C%20I%20get%20the%20login%20screens%20and%20after%20login%20everything%20works%20fine%20i.e.%20I%20can%20run%20Get-Team%20and%20Get-CsTeamsCallingPolicy%20cmdlets%20successfully%26nbsp%3Bbut%20I%20don't%20get%20this%20behavior%20when%20working%20with%20AadAccessToken.%3C%2FP%3E%3CP%3EAll%20the%20above%20code%20works%20fine%20if%20connecting%20to%20AzureAD%20module%20via%20Connect-AzureAD%20cmdlet%20like%20this%20both%20in%20web%20application%20and%20powershell%20editor%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EConnect-AzureAD%20-AadAccessToken%20%24AccessToken%20-AccountId%20'xxxxxxx%40xxxxxxx.onmicrosoft.com'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20someone%20has%20faced%20and%20successfully%20resolved%20this%20issue%20or%20have%20some%20tips%20on%20how%20to%20resolve%20this%2C%20please%20help.%3C%2FP%3E%3CP%3EI%20have%20already%20tried%20a%20lot%20of%20things%20including%20searching%20for%20the%20specific%20exception%20messages%20and%20any%20possible%20solutions%20but%20found%20nothing%20that%20could%20help%20in%20this%20particular%20scenario%2C%20installed%20the%20latest%20version%20of%20MSTeams%20module%2C%20the%20previous%20version%20was%20old%20and%20did%20not%20have%20all%20the%20cmdlets%20that%20I%20am%20looking%20to%20work%20with.%20I%20installed%20the%20preview%20version%20of%20MSTeams%20module%20also%20to%20see%20if%20this%20issue%20is%20fixed%20in%20the%20upcoming%20release.%20Uninstalled%20the%20deprecated%20SkypeForBuisnessOnline%20Connector%20module%2C%20updated%20windows%20and%20so%20on.%20If%20you%20look%20at%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fteams%2Fconnect-microsoftteams%3Fview%3Dteams-ps%23example-4%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EExample%204%20in%20the%20Microsoft%20documentation%20for%26nbsp%3BConnect-MicrosoftTeams%3C%2FA%3E%2C%20this%20is%20what%20I%20am%20trying%20to%20achieve.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2307796%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ec%20sharp%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Edot%20net%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EGraph%20API%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMFA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Graph%20Api%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%20CMDLETS%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVisual%20Studio%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2312356%22%20slang%3D%22en-US%22%3ERe%3A%20MicrosoftTeams%20cmdlets%20not%20working%20with%20AccessToken%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2312356%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1034361%22%20target%3D%22_blank%22%3E%40kshabih%3C%2FA%3E%26nbsp%3BThis%20seems%20to%20be%20something%20that%20Microsoft%20broke%20within%20the%20last%20day.%26nbsp%3B%20We%20are%20having%20the%20same%20issue.%26nbsp%3B%20%26nbsp%3BHave%20you%20opened%20a%20support%20case%20with%20Microsoft%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2319926%22%20slang%3D%22en-US%22%3ERe%3A%20MicrosoftTeams%20cmdlets%20not%20working%20with%20AccessToken%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2319926%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1041768%22%20target%3D%22_blank%22%3E%40et01267%3C%2FA%3E%20No%2C%20I%20have%20not%2C%20but%20seems%20like%20a%20good%20idea.%20Are%20you%20able%20to%20find%20a%20solution%20yet%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2321602%22%20slang%3D%22en-US%22%3ERe%3A%20MicrosoftTeams%20cmdlets%20not%20working%20with%20AccessToken%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2321602%22%20slang%3D%22en-US%22%3EIt%20seems%20that%20Microsoft%20has%20fixed%20(or%20reverted)%20whatever%20they%20broke%2C%20such%20that%20our%20config%20is%20now%20working%20after%20we%20switched%20back%20to%20the%20MicrosoftTeams%202.0%20module%20and%20the%20same%20authentication%20method%20as%20you.%3CBR%20%2F%3E%3CBR%20%2F%3ENote%20that%20there%20are%20a%20lot%20more%20threads%20around%20this%20issue%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fmicrosoftteams.uservoice.com%2Fforums%2F908686-bug-reports%2Fsuggestions%2F42946992-connect-microsoftteams%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmicrosoftteams.uservoice.com%2Fforums%2F908686-bug-reports%2Fsuggestions%2F42946992-connect-microsoftteams%3C%2FA%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fmicrosoftteams.uservoice.com%2Fforums%2F908686-bug-reports%2Fsuggestions%2F42973929-microsoftteams-powershell-module-v2-0-0-get-csonli%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmicrosoftteams.uservoice.com%2Fforums%2F908686-bug-reports%2Fsuggestions%2F42973929-microsoftteams-powershell-module-v2-0-0-get-csonli%3C%2FA%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmicrosoft-teams%2Fmicrosoft-teams-powershell-module-v2-3-set-csuser-broken%2Fm-p%2F2311166%2Fhighlight%2Ffalse%23M91536%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmicrosoft-teams%2Fmicrosoft-teams-powershell-module-v2-3-set-csuser-broken%2Fm-p%2F2311166%2Fhighlight%2Ffalse%23M91536%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2329101%22%20slang%3D%22en-US%22%3ERe%3A%20MicrosoftTeams%20cmdlets%20not%20working%20with%20AccessToken%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2329101%22%20slang%3D%22en-US%22%3EI%20am%20still%20getting%20the%20same%20errors.%20Thanks%20for%20sharing%20the%20links.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2333442%22%20slang%3D%22en-US%22%3ERe%3A%20MicrosoftTeams%20cmdlets%20not%20working%20with%20AccessToken%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2333442%22%20slang%3D%22en-US%22%3EWould%20you%20mind%20letting%20me%20see%20the%20actual%20code%20that%20works%20for%20you%3F%3C%2FLINGO-BODY%3E
New Contributor

I am trying to run cmdlets from powershell module MicrosoftTeams (version 2.0.0) in a C# web application. I am using Authorization code flow and code from the answer provided in this post to acquire token: Acquire AAD token using ASP.Net web forms. Note: I had changed resource in the code to graph.windows.net to acquire AAD token. Token is acquired by using AuthenticationContext.AcquireTokenByAuthorizationCodeAsync method. Once the token is acquired, I run the following lines to create a powershell instance in C# and to import MicrosoftTeams Module.

 

PowerShell pshell
InitialSessionState iss;
iss = InitialSessionState.CreateDefault2();
iss.ImportPSModule(new[] { "MicrosoftTeams" });
pshell = PowerShell.Create(iss);

 

Then to connect with MicrosoftTeams, I run the following code:

 

var connectCmd = new Command("Connect-MicrosoftTeams");
connectCmd.Parameters.Add("AadAccessToken", AccessToken);
connectCmd.Parameters.Add("AccountId", "xxxxxxx@xxxxxx.onmicrosoft.com");
pshell.Commands.AddCommand(connectCmd);
var result1 = pshell.Invoke();

 

Code works fine till here.

After this I clear the shell commands and invoke the Get-CsTeamsCallingPolicy cmdlet:

 

pshell.Commands.Clear();
pshell.Streams.Error.Clear();
    
pshell.AddScript("Get-CsTeamsCallingPolicy");
var result2 = pshell.Invoke();

 

After Invoke, I get an exception and this dialog pops up:

getcsinternalaccesstoken.png

Pressing 'Continue' brings back the same dialogue a couple of times.

Exception details from this screen are:

 

System.Collections.Generic.KeyNotFoundException was unhandled by user code
  HResult=-2146232969
  Message=The given key was not present in the dictionary.
  Source=mscorlib
  StackTrace:
       at System.Collections.Concurrent.ConcurrentDictionary`2.get_Item(TKey key)
       at Microsoft.TeamsCmdlets.Powershell.Connect.Models.AzureSessionProvider.GetAccessToken(String resource, IEnumerable`1 scopes) in D:\a\1\s\src\Microsoft.TeamsCmdlets.PowerShell.Connect\Models\AzureSession.cs:line 80
       at Microsoft.TeamsCmdlets.Powershell.Connect.TeamsPowerShellSession.GetAccessToken(String resource, IEnumerable`1 scopes) in D:\a\1\s\src\Microsoft.TeamsCmdlets.PowerShell.Connect\TeamsPowerShellSession.cs:line 82
       at Microsoft.TeamsCmdlets.PowerShell.Connect.GetCsInternalAccessToken.ProcessRecord() in D:\a\1\s\src\Microsoft.TeamsCmdlets.PowerShell.Connect\GetCsInternalAccessToken.cs:line 61
       at System.Management.Automation.CommandProcessor.ProcessRecord()

 

After pressing continue for the 3rd time, control goes back to C# code, and I receive the following runtime exception:

 

Exception calling "GetSteppablePipeline" with "1" argument(s): 
"Exception calling "GetRemoteNewCsOnlineSession" with "1" argument(s): 
"Run either Connect-MicrosoftTeams or new-csonlinesession before running cmdlets.""

 

Trying to run this logic from the powershell editor shows similar behavior:

Running the following two lines:

 

$AccessToken =  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
Connect-MicrosoftTeams -AadAccessToken $AccessToken -AccountId 'xxxxxxx@xxxxxx.onmicrosoft.com'

 

gives this result:

 

Account               Environment Tenant                               TenantId                            
-------                         ----------- ------                               --------                            
xxxxxxx@xxxxxx.onmicrosoft.com AzureCloud  xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

 

I then run Get-Team cmdlet:

 

Get-Team -User xxxxxxx@xxxxxxx.onmicrosoft.com

 

which results in this message:

 

Get-Team : The given key was not present in the dictionary.
 At line:1 char:1
 + Get-Team -User xxxxxxx@xxxxxxx.onmicrosoft.com
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (:) [Get-Team], KeyNotFoundException
     + FullyQualifiedErrorId : System.Collections.Generic.KeyNotFoundException,Microsoft.TeamsCmdlets.PowerShell.Custom.GetTeam

 

Running cmdlet Get-CsTeamsCallingPolicy yields this:

 

Exception calling "GetSteppablePipeline" with "1" argument(s): "Exception calling "GetRemoteNewCsOnlineSession" with "1" argument(s): "Run either Connect-MicrosoftTeams or new-csonlinesession before running cmdlets.""
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.0.0\net472\SfBORemotePowershellModule.psm1:11369 char:13
+ $steppablePipeline = $scriptCmd.GetSteppablePipeline($myI ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CmdletInvocationException

 

If I run Connect-MicrosoftTeams directly from powershell without providing access token and accountid, I get the login screens and after login everything works fine i.e. I can run Get-Team and Get-CsTeamsCallingPolicy cmdlets successfully but I don't get this behavior when working with AadAccessToken.

All the above code works fine if connecting to AzureAD module via Connect-AzureAD cmdlet like this both in web application and powershell editor:

 

Connect-AzureAD -AadAccessToken $AccessToken -AccountId 'xxxxxxx@xxxxxxx.onmicrosoft.com'

 

If someone has faced and successfully resolved this issue or have some tips on how to resolve this, please help.

I have already tried a lot of things including searching for the specific exception messages and any possible solutions but found nothing that could help in this particular scenario, installed the latest version of MSTeams module, the previous version was old and did not have all the cmdlets that I am looking to work with. I installed the preview version of MSTeams module also to see if this issue is fixed in the upcoming release. Uninstalled the deprecated SkypeForBuisnessOnline Connector module, updated windows and so on. If you look at Example 4 in the Microsoft documentation for Connect-MicrosoftTeams, this is what I am trying to achieve.

5 Replies

@kshabih This seems to be something that Microsoft broke within the last day.  We are having the same issue.   Have you opened a support case with Microsoft?

 

@et01267 No, I have not, but seems like a good idea. Are you able to find a solution yet?
It seems that Microsoft has fixed (or reverted) whatever they broke, such that our config is now working after we switched back to the MicrosoftTeams 2.0 module and the same authentication method as you.

Note that there are a lot more threads around this issue:
https://microsoftteams.uservoice.com/forums/908686-bug-reports/suggestions/42946992-connect-microsof...
https://microsoftteams.uservoice.com/forums/908686-bug-reports/suggestions/42973929-microsoftteams-p...
https://techcommunity.microsoft.com/t5/microsoft-teams/microsoft-teams-powershell-module-v2-3-set-cs...

I am still getting the same errors. Thanks for sharing the links.
Would you mind letting me see the actual code that works for you?