[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Powershell script to multithread imapsync on Windows


From CARTER Alex <alex dot carter at ed dot ac dot uk>
Subject Powershell script to multithread imapsync on Windows
Date Fri, 6 Mar 2015 15:19:29 +0000

I've attached a powershell script for multi-threaded mail copy on windows  
for those who are interested.

It takes a csv input and copies in a fixed number of background threads, 
then summarises the results by extracting from the per-user log files.


You will need to update anything in square brackets [ ], specifically:
csv file location
host1
host2
directory for log files

Plus the patch to imapsync itself

I've set a default of 16 max threads, which seems to work well on a 
machine with c8GB RAM but experiment to find your own level.

You would also want to specify more options for the copy of course.

Hope someone finds this useful.

Alex
--
Alex Carter
Head of Service Management
Applications Division
The University of Edinburgh

+44 (0)131 651 4414



-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

#
# This script uses imapsync to perform a mail sync for a batch of users
#

# Set maximum threads for simulataneous mail copy
$MaxThreads = 16

####################################################
#STEP 1
###################################################
#get data file of users to be synchronised
# data format & 1st line: user1,user2,password1,password2

$csv = @(Import-CSV [Path to file])

####################################################
#STEP 2
###################################################
# Define Sync job
# any other commands needed additional parameters can be added as long as they are included in the job call
#
# Update to path of imapsync.exe on your computer

$sync_job = { Param($user1,$user2,$userlogfile)
  E:\Scripts\imapsync  --host1 [host1] --port1 993 --ssl1 --authmech1 PLAIN --user1 $user1 --password1  PWCHANGED `
    --host2 [host2] --port2 993 --ssl2 --authmech2 PLAIN --user2 $user2 --password2  PWCHANGED `
    --logfile $userlogfile 
}

####################################################
#STEP 3
###################################################
# start mail sync

# start mail sync for each user up to maxthreads +1 count, create logs list array for displaying results later
$logs_list=@()

foreach ($line in $csv) {

  $user1=$line.user1
  $user2=$line.user2
  $userlogfile = '[Path to logs]' + $user1 + '.log'

  write-output "Initiating sync for $($line.user1) detailed log will be output to $userlogfile"

  Start-Job -ScriptBlock $sync_job -ArgumentList @($user1, $user2, $userlogfile) | Out-Null
  $logs_list += $userlogfile

  While($(Get-Job -State 'Running').Count -gt $MaxThreads) {
      sleep 10
  }
}

write-output "All sync jobs started. Awaiting completion, then results will be displayed"

Get-Job | Wait-Job | out-null

####################################################
#STEP 4
###################################################
# Display sumamry results of the sync & add to log file
# in this example the tail (last 5 lines) is selected and then the 
# number of errors and path of the log file retured to Powershell

for ($i=0; $i -lt $logs_list.length; $i++) {
  write-output "- - -"  
	Get-Content $logs_list[$i] -tail 5 | Select-Object -Index 0,4 
}