Forum Discussion

AntJH8's avatar
AntJH8
Copper Contributor
Jun 09, 2021

Running a script keeps stopping every couple of minutes to create a new remote powershell session

Hi,

 

I'm trying to run a script that uses Set-Mailbox and Set-MsolUser. Every 2 minutes or so I get the following and then script carries on:

 

Creating a new Remote PowerShell session using MFA for implicit remoting of "Set-Mailbox" command

 

Then after about 10 minutes the script will crash with this error:

 

Starting a command on the remote server failed with the following error message : The I/O operation has been aborted because of either a thread exit or an application request. For more information, see the about_Remote_Troubleshooting
Help topic.
+ CategoryInfo : OperationStopped: (outlook.office365.com:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure
+ PSComputerName : outlook.office365.com

 

Any ideas as I've tried changing the PSSession idletimeout. 

2 Replies

    • AntJH8's avatar
      AntJH8
      Copper Contributor
      Hi Vasil,
      I had a look at that article. I'm not running anything crazy, it's all updates on individual mailboxes. Yes we do have 12,000+ mailboxes. But this script was only processing a few accounts., see below:


      $users = Import-CSV "C:\mydir\list.csv"
      Foreach ($user in $users){
      $user.UserPrincipalName
      #Convert mailbox to shared and hide from the address list
      Set-Mailbox -identity $user.UserPrincipalName -Type shared -HiddenFromAddressListsEnabled $true
      Start-Sleep -milliseconds 500
      #Get the list of licences
      $licenses = Get-MsolUser -UserPrincipalName $user.UserPrincipalName | select -ExpandProperty licenses
      Start-Sleep -milliseconds 500
      #Block sign-in to the account
      Set-MsolUser -UserPrincipalname $user.UserPrincipalName -UsageLocation IE -BlockCredential $true
      Start-Sleep -milliseconds 500
      #Remove all licenses from the account
      Foreach ($license in $licenses){
      Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicense $license.AccountSkuId
      Start-Sleep -milliseconds 500
      }
      }

Resources