robocopy says its receiving invalid parameters in when used in powershell script

6,395

It's likely you can reproduce your problem by trying the following in PowerShell:

robocopy a b * "/e /purge"

If this is true, parameter expansion is what's causing your problem. To work around this, you can go this route:

iex "$srcLocation $destLocation $files $options"
Share:
6,395
FilBot3
Author by

FilBot3

I'm a DevOps Engineer with a background in Systems Engineering, education in Network Engineering and hobby in Development. I like to use Ruby, Golang, Python, and C#. I also do a little Vimscript.

Updated on September 18, 2022

Comments

  • FilBot3
    FilBot3 almost 2 years

    I'm attempting to use robocopy in a PowerShell script, but currently failing. When I run the following command manually

    robocopy \\corporatesystem00.local.com\C$\Maven\ \\corporatesystem01.local.com\C$\Maven\ * /e /purge
    

    I am able to copy all the contents of one folder to another, and it works like magic. However, with how "wonderful" PowerShell is, I think I'm running into an unknown gotchya. When I run the same command above from a PowerShell script, I receive this error:

    PS C:\Program_Files> .\jenkins-maven-copy.ps1
    Performing Jenkins Maven Sync
    robocopy \\corporatesystem00.local.com\C$\Maven\ \\corporatesystem01.local.com\C$\Maven\ * /e /purge
    
    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows
    -------------------------------------------------------------------------------
    
      Started : Wednesday, July 30, 2014 3:00:03 PM
       Source - \\corporatesystem00.local.com\C$\Maven\
         Dest - \\corporatesystem01.local.com\C$\Maven\
    
        Files : *
    
      Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30
    
    ------------------------------------------------------------------------------
    
    ERROR : Invalid Parameter #4 : "/e /purge"
    
           Simple Usage :: ROBOCOPY source destination /MIR
    
                 source :: Source Directory (drive:\path or \\server\share\path).
            destination :: Destination Dir  (drive:\path or \\server\share\path).
                   /MIR :: Mirror a complete directory tree.
    
        For more usage information run ROBOCOPY /?
    
    
    ****  /MIR can DELETE files as well as copy them !
    

    This happens for any commandline option I'm putting in for my options in the script. The script I am running, is below.

    Param (
        [string]$srcLocation = '\\corporatesystem00.local.com\C$\Maven\',
        [string]$destLocation = '\\corporatesystem01.local.com\C$\Maven\',
        [string]$files = '*',
        [string]$options = '/e /purge'
    )
    
    function syncMaven( $srcLocation, $destLocation, $files, $options )
    {
        Write-Host "Performing Jenkins Maven Sync"
        Write-Host "robocopy $srcLocation $destLocation $files $options"
        robocopy $srcLocation $destLocation $files $options
    }
    
    syncMaven $srcLocation $destLocation $files $options
    

    If you look at the error/output from earlier, it shows that the command is being passed, with the command options, but robocopy isn't recognizing them or something.

    PS C:\Program_Files> .\jenkins-maven-copy.ps1
    Performing Jenkins Maven Sync
    robocopy \\corporatesystem00.local.com\C$\Maven\ \\corporatesystem01.local.com\C$\Maven\ * /e /purge
    

    So, I'm at a loss.

  • FilBot3
    FilBot3 almost 10 years
    what does the iex option do?
  • FilBot3
    FilBot3 almost 10 years
  • FilBot3
    FilBot3 almost 10 years
  • FilBot3
    FilBot3 almost 10 years
    I will, once I get to work. I don't have access to what I need to test.