12 March 2013

Powershell: Robocopy Invalid Parameter Error

Posted By: Mick Pletcher - 4:14 PM

Share

& Comment

It is nice having robocopy integrated with PowerShell, but there are some quirks that can make it a pain to deal with. While I was writing a Powershell script that will automate the robocopy, I began getting the Invalid Parameter error. I went through troubleshooting it and made sure it was exactly the same I was typing in at the command line. I still could not figure it out until I split it up.

 $Source = "..\na_clients"  
 $Dest = "..\"+$Location  
 $Files = "*.*"  
 $Switches = "/R:0 /W:0 /E /MIR /TEE /Z /TIMFIX"
 $Logs = "/LOG:..\0ReplicationLogs\"+$Location+".log"  
 $ExcludeDIR = "0Archive","0ReplicationLogs","DfsrPrivate","DesignPremium32Bit","Indesign32bit"  
 RoboCopy $Source $Dest $Files $Switches /XD $ExcludeDIR[0] $ExcludeDIR[1] $ExcludeDIR[2] $ExcludeDIR[3] $ExcludeDIR[4]  

This is where I split up the robocopy command line into variables for each of the switches. That fixed the issue. Apparently, Powershell does not allow for you to be able to use spaces between the switches within a quoted variable. I went as far as dividing up the $Switches variable with a +[char]32+ between each quoted switch and it did no good. This below is the only solution I came up with.

 $Source = "..\na_clients"  
 $Dest = "..\"+$Location  
 $Files = "*.*"  
 $Retries = "/R:0"  
 $Wait = "/W:0"  
 $Logs = "/LOG:..\0ReplicationLogs\"+$Location+".log"  
 $CopySubfolders = "/E"  
 $Mirror = "/MIR"  
 $ConsoleWin = "/TEE"  
 $RestartMode = "/Z"  
 $FixTimeStamp = "/TIMFIX"  
 $ExcludeDIR = "0Archive","0ReplicationLogs","DfsrPrivate","DesignPremium32Bit","Indesign32bit"  
 RoboCopy $Source $Dest $Files $IPG $Retries $Wait $Logs $CopySubfolders $Mirror $ConsoleWin $RestartMode $FixTimeStamp /XD $ExcludeDIR[0] $ExcludeDIR[1] $ExcludeDIR[2] $ExcludeDIR[3] $ExcludeDIR[4]  


About Mick Pletcher

My blog is here to help solve issues I have encountered and solved, publish scripts I have written, and educate others in understanding areas that are not well covered

3 comments:

  1. My solution was to construct the entire robocopy command line as a string and then use Invoke-Expression on the entire

    ReplyDelete
  2. I did think about that as a solution, but I decided to go with the variables for easy editing by other users.

    ReplyDelete
  3. Very Helpful. Exactly the solution I needed

    ReplyDelete

Copyright © 2013 Mick's IT Blogs™ is a registered trademark.