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

%3CLINGO-SUB%20id%3D%22lingo-sub-2431686%22%20slang%3D%22en-US%22%3ERunning%20a%20script%20keeps%20stopping%20every%20couple%20of%20minutes%20to%20create%20a%20new%20remote%20powershell%20session%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2431686%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20run%20a%20script%20that%20uses%20Set-Mailbox%20and%20Set-MsolUser.%20Every%202%20minutes%20or%20so%20I%20get%20the%20following%20and%20then%20script%20carries%20on%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3ECreating%20a%20new%20Remote%20PowerShell%20session%20using%20MFA%20for%20implicit%20remoting%20of%20%22Set-Mailbox%22%20command%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20after%20about%2010%20minutes%20the%20script%20will%20crash%20with%20this%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EStarting%20a%20command%20on%20the%20remote%20server%20failed%20with%20the%20following%20error%20message%20%3A%20The%20I%2FO%20operation%20has%20been%20aborted%20because%20of%20either%20a%20thread%20exit%20or%20an%20application%20request.%20For%20more%20information%2C%20see%20the%20about_Remote_Troubleshooting%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EHelp%20topic.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20CategoryInfo%20%3A%20OperationStopped%3A%20(outlook.office365.com%3AString)%20%5B%5D%2C%20PSRemotingTransportException%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20FullyQualifiedErrorId%20%3A%20JobFailure%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%2B%20PSComputerName%20%3A%20outlook.office365.com%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%20as%20I've%20tried%20changing%20the%20PSSession%20idletimeout.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2431686%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2431950%22%20slang%3D%22en-US%22%3ERe%3A%20Running%20a%20script%20keeps%20stopping%20every%20couple%20of%20minutes%20to%20create%20a%20new%20remote%20powershell%20sessio%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2431950%22%20slang%3D%22en-US%22%3EThat's%20the%20reality%20of%20ExO%20remote%20PowerShell%2C%20especially%20if%20you%20don't%20handle%20throttling.%20Read%20here%20for%20some%20guidance%2Fsamples%20on%20how%20to%20do%20it%20%22properly%22%3A%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexchange-team-blog%2Frunning-powershell-cmdlets-for-large-numbers-of-users-in-office%2Fba-p%2F604280%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexchange-team-blog%2Frunning-powershell-cmdlets-for-large-numbers-of-users-in-office%2Fba-p%2F604280%3C%2FA%3E%3C%2FLINGO-BODY%3E
New Contributor

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
That's the reality of ExO remote PowerShell, especially if you don't handle throttling. Read here for some guidance/samples on how to do it "properly": https://techcommunity.microsoft.com/t5/exchange-team-blog/running-powershell-cmdlets-for-large-numbe...
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
}
}