0

I am running into an strange issue building a Powershell script. I want to programatically connecto to Microsoft Teams and execute some skype for businesss online commands.

When I simply do on a terminal

Connect-MicrosoftTeam and get prompted and import my credentials, it all works perfectly, but i need to this programatically.

I did an app registration with the following permissions

API Registration permissions

And I am authenticating against Azure AD and Graph:

#Connect to Graph
$Body = @{    
Grant_Type    = "client_credentials"
Scope         = "https://graph.microsoft.com/.default"
client_Id     = $ApplicationID
Client_Secret = $AccessSecret
} 

$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token" -Method POST -Body $Body
$token = $ConnectGraph.access_token
#Connect to AD
$uri = "https://login.microsoftonline.com/${tenant_id}/oauth2/token"
$body = @{grant_type='refresh_token';resource='https://graph.windows.net';client_id=$clientId;refresh_token=$refresh_token}
$result = Invoke-RestMethod -Method Post -Uri $uri -Body $body
$accessToken = $result.access_token

Then I run

Connect-MicrosoftTeams -AadAccessToken $accessToken -MsAccessToken $token -AccountId $account

And it doesn't throw an error, works properly. and i gen run commands like Get-Team and returns the info correctly. However when I try to run an skype command like

 Get-CsOnlineVoiceUser

I get the error

Get-CsOnlineSession : Run Connect-MicrosoftTeams before running cmdlets.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:63 char:22
+     $remoteSession = & (Get-CsOnlineSessionCommand)
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-CsOnlineSession], UnauthorizedAccessException
    + FullyQualifiedErrorId : UnauthorizedAccessException,Microsoft.Teams.ConfigApi.Cmdlets.GetCsOnlineSession
 
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:10006 char:38
+ ...    -Session (Get-PSImplicitRemotingSession -CommandName 'Get-CsOnline ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Am I missing something? I am using the 2.3.1 Teams Module.

Thanks!

di3go
  • 133
  • 1
  • 1
  • 5
  • Could you please update your teams module to the latest version at [microsoft teams powershell module to 2.4.0](https://www.powershellgallery.com/packages/MicrosoftTeams/2.4.0-preview) and see if that helps. Could you also take a look at [Microsoft Teams access policy - New-CsApplicationAccessPolicy returns 404](https://stackoverflow.com/questions/68249596/microsoft-teams-access-policy-new-csapplicationaccesspolicy-returns-404/68351427?noredirect=1#comment120806127_68351427) where they got a similar session error. – Hunaid Hanfee-MSFT Jul 16 '21 at 13:21

1 Answers1

0

This is a bit tricky to do, but can be achieved.

I recommend reading this blog post for exact instructions and code examples: https://paulobrien.co.nz/2020/06/29/s4b-online-powershell-modern-auth/

Should get you started.

  • Ah! thanks for the quick response, however, it seems that New-CsOnlineSession is now deprecated as per MS documentation and it is only avialaible the SkypeBusinessOnline module which will be removed on July 13. https://learn.microsoft.com/en-us/powershell/module/skype/new-csonlinesession?view=skype-ps – di3go Jul 13 '21 at 14:14