Forum Discussion

balaji94's avatar
balaji94
Copper Contributor
Jan 28, 2024

Unable to Execute PowerShell Script Commands in Microsoft Teams Session Established via Script

I encountered an issue while attempting to execute PowerShell script commands within a Microsoft Teams session established via a script. The script includes commands to connect to Microsoft Teams using the Connect-MicrosoftTeams cmdlet and subsequently execute other Teams-related commands.

While the script executes without errors, the session does not seem to be fully established, resulting in the following error when attempting to execute subsequent commands:

powershell : Get-CsTeamsClientConfiguration : Session is not established, run Connect-MicrosoftTeams before requesting access token
At line:1 char:1
+ powershell -File 'C:\Users\***********************\script.p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Get-CsTeamsClie...ng access token:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

At C:\Users\****************************\script.ps1:8 char:1
+ Get-CsTeamsClientConfiguration
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-CsTeamsClientConfiguration], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Teams.ConfigApi.Cmdlets.GetCsTeamsClientCon
figuration

 

This issue is inconsistent, as the commands execute successfully when executed manually. Additionally, I've attempted to introduce delays in the script to allow for the session to fully establish, but the issue persists.

This issue impacts the ability to automate tasks in Microsoft Teams using PowerShell scripts.

Please let me know if there are any additional steps or information needed to address this issue effectively.

Resources