User Powershell in conjunction with robocopy

I want to run Robocopy via powershell because I inadvertently terminated the robocopy process on a very large vol and I want to start off a certain parent folder.

I have a script that uses get-childitem and I want to do a FOREACH loop and run Robocopy and wait until complete to move on to the next parent folder.

$paths = Get-ChildItem -Path \\server\dir -Directory | ?{$_.Name -match '^[M-Z]'} |Sort-Object 

Foreach ($path in $paths)
{
$path.Name
$source = $path.FullName
$dest = "E:\Netapp01\Images01\" + $path.Name
$logFile = "/LOG:C:\temp\netapp01_Image01_Eclipse_\" + $path.Name + ".log"
$Switches = "/NDL /NFL /S /E /XO /DCOPY:DA /COPY:DAT  /MIR /R:1 /W:1 /Log:$LogFile"


#TO DO
#  I want to run Robocopy for each $path and wait for it to complete then
# move on to the next $path




}

Open in new window

LVL 27
yo_beeDirector of Information TechnologyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Collect the switches in an array, and just run robocopy:
$paths = Get-ChildItem -Path \\server\dir -Directory | Where-Object {$_.Name -match '^[M-Z]'} | Sort-Object -Property Name
$switches = '/NDL', '/NFL', '/S', '/E', '/XO', '/DCOPY:DA', '/COPY:DAT', '/MIR', '/R:1', '/W:1'
Foreach ($path in $paths) {
	$path.Name
	$source = $path.FullName
	$dest = "E:\Netapp01\Images01\" + $path.Name
	$logFile = "/LOG:C:\temp\netapp01_Image01_Eclipse_\" + $path.Name + ".log"
	& robocopy.exe $source $dest $switches "/Log:`"$($LogFile)`""
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yo_beeDirector of Information TechnologyAuthor Commented:
Thanks OBda.  

So by doing & keeps the single thread alive until complete?
0
David Johnson, CD, MVPOwnerCommented:
I would use
$arglist = $source + " " + $dest + " " + $switches + " /LOG: $(LogFile)"
start-process -FilePath robocopy.exe  -ArgumentList $args

Open in new window

if you add /wait then the current script will pause (wait) until the process exits
1
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

oBdACommented:
When calling external command line programs, PowerShell will just wait by default until they're finished, just like cmd.exe does. Nothing fancy you need to do to achieve that.
It's called PowerShell because it's not only a scripting engine, but a shell as well, and as such supports calling external programs without further ado. Make use of it.
Technically, you don't need the "&" at all, but it's recommended when calling external programs (as well as using the .exe extension) to make it clear what's happening (that is, an external program and not a cmdlet/function/alias is called).
The "&" would be required if the external program to call is part of a string.
"C:\Windows\system32\robocopy.exe" would be considered a simple string by PowerShell and sent to stdout/the pipeline.
To run that, the ampersand is required:
& "C:\Windows\system32\robocopy.exe"
1
yo_beeDirector of Information TechnologyAuthor Commented:
Thanks for your insight and help.

I had to tweak the switches a bit because /MIR is the same as /e /purge so I removed /xo.
I also had to tweak the log switch a bit, but it is running and I am a happy camper
1
Senior IT System EngineerIT ProfessionalCommented:
What is the difference when you just running or executing the Robocopy command directly with all of the parameters without the Powershell script ?

From memory, you can just retry running the Robocopy script because it can intelligently skip which files already copied.
0
yo_beeDirector of Information TechnologyAuthor Commented:
@IT System  

I would agree majority of the time, this one is a is a very large vol that is probably only 50% through when I inadvertently closed the command prompt. This process already  was on the 15th day of coping and I did not what to traverse all the previously mirrored directories.


8 TB made up of 117 million files.  It's a beast.  I probably saved myself 3 or so days
1
David Johnson, CD, MVPOwnerCommented:
use the robocopy switch /xo (exclude older files)
1
yo_beeDirector of Information TechnologyAuthor Commented:
it still needs to traverse the directories that I did already.  I know it runs through it pretty fast, but I do not need to even consider those at this time.  My primary goal is to make a complete secondary copy of the data and if I had to run Robocopy from the beginning even with the /xo switch I would be wasting valuable time that I do not have.  

I know RC very well.  I have been using it for 12 or more years.  This one is just two large of a volume to start from scratch.
1
yo_beeDirector of Information TechnologyAuthor Commented:
Follow Up on just one of the parent folders that I had to start over with.  As you can see it took almost 4 hours to just get passed all the files that were already copied.  This is why I could not start over from the beginning.  At the point I stopped RC it was already running for 15 days.


               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :      6039      1883      4156         0         0         0
   Files :  20751798   6915726  13836072         0         0         0
   Bytes : 503.919 g 168.185 g 335.733 g         0         0         0
   Times :  33:32:28  29:37:56                       0:00:00   3:54:32


   Speed :             1692865 Bytes/sec.
   Speed :              96.866 MegaBytes/min.
   Ended : Wednesday, May 2, 2018 4:31:32 AM

As you can see
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.