MicrosoftTeams cmdlets not working with AccessToken

Copper 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 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", "");
var result1 = pshell.Invoke();


Code works fine till here.

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


var result2 = pshell.Invoke();


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


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
  Message=The given key was not present in the dictionary.
       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 ''


gives this result:


Account               Environment Tenant                               TenantId                            
-------                         ----------- ------                               --------                    AzureCloud  xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx


I then run Get-Team cmdlet:


Get-Team -User


which results in this message:


Get-Team : The given key was not present in the dictionary.
 At line:1 char:1
 + Get-Team -User
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + 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 ''


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:

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?