Forum Discussion
AntJH8
Jun 09, 2021Copper Contributor
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
Sort By
- 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-numbers-of-users-in-office/ba-p/604280
- AntJH8Copper ContributorHi 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
}
}