0

I am trying to use Azure RunBook with PowerShell to manage the call forwarding status for Teams users. I use an Azure app that has (I believe) all required Graph API permissions:

Graph API permissions

And is also set as Teams Administrator.

Important note: I'm running it in 5.1 version, the 7.1 preview kept saying that I have incorrect access rights, which was a bull

The Runbook PowerShell code is as follow:

#Import Modules
Import-Module MSAL.PS 
Import-Module MicrosoftTeams  

#Clear Tokencache
Clear-MsalTokenCache 

#Variables
$TenantId = "TenantID of my app"
$ClientID = "ClientId  of my app" #DemoTeamsPS app
$ClientSecret ="generated secret"


$graphtokenBody = @{   
   Grant_Type    = "client_credentials"   
   Scope         = "https://graph.microsoft.com/.default"   
   Client_Id     = $ClientID
   Client_Secret = $ClientSecret   
}  

$graphToken = Invoke-RestMethod -Uri 
"https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body 
$graphtokenBody | Select-Object -ExpandProperty Access_Token 

$teamstokenBody = @{   
   Grant_Type    = "client_credentials"   
   Scope         = "48ac35b8-9aa8-4d74-927d-1f4a14a0b239/.default"   
   Client_Id     = $ClientID 
   Client_Secret = $ClientSecret 
} 

$teamsToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body $teamstokenBody | Select-Object -ExpandProperty Access_Token 

Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")  

Get-CsOnlineUser -Identity me@myOrganisation.com | fl *Ent*,*host*,*voice*, *um*

Up to this line, everything works fine, the test pane returns my details. However, I am trying to change the call forwarding rules. Hence the two lines:

Set-CsUserCallingSettings -Identity me@myOrganisation.com  -IsUnansweredEnabled $FALSE
Set-CsUserCallingSettings -Identity me@myOrganisation.com  -IsForwardingEnabled $true -ForwardingType Immediate -ForwardingTargetType SingleTarget -ForwardingTarget "+1234567891"

But this returns an error "The stream was already consumed. It cannot be read again" (twice actually, one for each line).

Do you have any ideas?

Note: using

 Get-CsUserCallingSettings -Identity me@mycompany.com

returns the same error

Yasskier
  • 791
  • 1
  • 14
  • 36
  • In c# the solution is to put the code in a using block so the object gets disposed. So you close the first connection before attempting the second. Here is the PS solution : https://stackoverflow.com/questions/42107851/how-to-implement-using-statement-in-powershell – jdweng Mar 02 '23 at 22:44
  • @jdweng Thanks, but I don't think that is the issue, after using "Connect-MicrosoftTeams" I am connected until either some hidden timeout or disconnect command, so I should be definitely still connected when the "Set-CsUserCallingSetting" is modified. – Yasskier Mar 02 '23 at 22:53
  • The title says "The stream was already consumed". You code is trying to use the stream a second time. A HTTP Request/Response must close and open a new connection for each transition. So you must make sure the connection is disposed before and second attempt is done. – jdweng Mar 03 '23 at 07:27
  • @jdweng OK... **how would I do that?** There is a "CONNECT" command and I can put the SET-CSUserCallSettings straight after with similar results, whie I can put many other commands like GET-CSOnlineUser. – Yasskier Mar 04 '23 at 06:16
  • Simple way is to have a variable $running (true/false) and put before and after the call to azure. Then display message if user runs one command before another finishes. Connect to can set to $null at beginning of code and then test if it is not $null before trying a connection so you do not get two connection using same stream. – jdweng Mar 04 '23 at 09:09

0 Answers0