PowerShell – Robocopy launching multiple concurrent sessions

This script has been derived from below VB script:

Robocopy launching multiple concurrent sessions

https://gallery.technet.microsoft.com/scriptcenter/Robocopy-launching-f67faec7

It has been requested by many of my followers/Colleagues to change it to powershell as it sounds easy to do modifications.

So here we go, download & extract the zip file from below location.

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Robocopy-1b4e1cec

Update the Source & Destination in sd.CSV file to mirror using Robocopy

There is a timeout 60 inside the .ps1 script which you can update as per your requirement, this has been done to control the number of sessions.

Every 60 seconds new robocopy session is launched so please time it as per scenario.

We have ran the script from:

C:\SyncRobocopy

if you wish to change this path than please change the it inside the rcpy.bat file as well

robocopy %1 %2 /MIR /copyall /r:4 /w:1 /zb /fp  /V /ts | find /i /v “same” >> C:\SyncRobocopy\logs\%3_Share_1.txt

All logs will be saved inside the logs folder , robocopy will only log new files or files that are extra

You can manully run mirr.bat or schedule it as we have done using task scheduler.

Note:- Script has a logic to recycle logs after 30 days.

################################################################ 
#             Author: Vikas Sukhija 
#            Date: 02/05/2016 
#            Update: 
#            Desc: Sync directories 
# 
################################################################ 
 
$date = get-date -format d 
# replace \ by - 
$time = get-date -format t 
$month = get-date  
$month1 = $month.month 
$year1 = $month.year 
 
 
$date = $date.ToString().Replace(“/”, “-”) 
 
$time = $time.ToString().Replace(":""-"$time = $time.ToString().Replace(" """) 
 
$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.log" 
 
$regex = "(.*)\\" 
 
$path = ".\logs\" 
$limit = (Get-Date).AddDays(-30) #for log recycling 
 
start-transcript -Path $logs 
 
$data = import-csv .\sd.csv 
 
foreach($dt in $data){ 
 
$source = $dt.source 
$destination = $dt.destination 
 
$rblogs = $source -replace $regex 
 
write-host "Robocopy started for source $source and destionation $destination" -foregroundcolor yellow 
 
start-process powershell ".\rcpy.bat $source $destination $rblogs" 
 
timeout 60 
 
 
write-host "Robocopy Process for source $source and destionation $destination invoked" -foregroundcolor green 
 
} 
 
########################Recycle logs ###################################### 
 
Get-ChildItem -Path $path  | Where-Object {   
$_.CreationTime -lt $limit } | Remove-Item -recurse -Force  
 
 
Stop-transcript 
 
##################################################################

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

One thought on “PowerShell – Robocopy launching multiple concurrent sessions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s