• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 67
  • Last Modified:

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

1
yo_bee
Asked:
yo_bee
  • 5
  • 2
  • 2
  • +1
1 Solution
 
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
 
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
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now