Issue in script




I've created some script to move users from SfB On Prem to Teams Only mode.


In script I create connection to SfB Online and O365. But after 1 hour script fails - something related to token, I think Azure AD one.


Error is in screenshot.


Here is my connection code part:


Import-Module "C:\\Program Files\\Common Files\\Skype for Business Online\\Modules\\SkypeOnlineConnector\\SkypeOnlineConnector.psd1"

$password = ConvertTo-SecureString “xxx” -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential (“xxx”, $password)

$global:session = New-CsOnlineSession -Credential $credential -OverrideAdminDomain
Import-PSSession $session

Connect-MsolService -Credential $Credential



How to resolve that issue because it's little annoying rerun script ebvery 1 hour :) I setup that option Enable-CsOnlineSessionForReconnection but it doesn't help



5 Replies

@adam deltinger I have that command in my script. And in simple loop script it works.


But I think issue is with 


I think that token expires in 1 hour 


Which command do you use for migrating the users to Teams? As of CU8 for SfB 2015 and SfB 2019, you can use the following command: Move-CsUser -Identity "" -Target "" -MoveToTeams -Credential $cred

That command doesn't need a connection to msolservice or Skype Online. Therefor you also won't run into the timing issue.


If you still need to use the Skype Online module, you could use something like this:


$StartDate = Get-Date
foreach($user in $users){
    $CurrentDate = Get-Date
    If ($CurrentDate -ge $StartDate.AddMinutes(45)){
        $sfbSession = New-CsOnlineSession -Credential $cred
        Import-PSSession $sfbSession -AllowClobber -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
        $StartDate = Get-Date
Grant-CsTeamsUpgradePolicy -identity $user.UPN -policyname UpgradeToTeams -confirm:$false

To be on the safe side, it will reconnect 45 minutes after the first connection.



I use that:


Move-CsUser -Identity $user -Target -HostedMigrationOverrideUrl -ProxyPool -MoveToTeams -BypassAudioConferencingCheck -BypassEnterpriseVoiceCheck -credential $credential -Verbose -Confirm:$false

@msabat That looks fine to me. And in my experience, there's no issue with token lifetimes when using that command.


The error message is regarding the command Get-CsOnlineUser. Perhaps you can work around that command so you won't need it. What are you using that command for?