12 March 2013

Powershell: Robocopy Invalid Parameter Error

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]  


4 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
  4. I dont like the errors this software gives, it feels like I am programming. To be honest, I liked this software at first but then it started to be troublesome. I replaced this software with GS Richcopy 360 and it gave me freedom from the errors. Now I dont have to google every time while transferring files to server or at the time of backup. You should also try it for a stress-free life. Thanks!

    ReplyDelete