Powershell + Robocopy

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of NetAdminTX NetAdminTX 1 month, 2 weeks ago.

  • Author
    Posts
  • #62040
    Profile photo of NetAdminTX
    NetAdminTX
    Participant

    I know this is not strictly a powershell topic, but I am trying to use a powershell script to check for existence of files, and then if they don't exist copy them from a network location to the current users %appdata% subdirectories
    I have listed the code below and the error I am getting, I have tried multiple variations of this code.
    Your assistance is greatly appreciated.

    If (test-path $env:APPDATA"\microsoft\signatures\citywide signature.txt") {exit}
    $source= "\\addomainname\SYSVOL\deerpark.local\scripts\Citywide Signature\"
    $Destination= $env:APPDATA + "\microsoft\signatures\"
    $options= ("/r:5","/w:10","/e")
    invoke-expression "robocopy $source $destination $options"

    ——————————————————————————-
    ROBOCOPY :: Robust File Copy for Windows
    ——————————————————————————-

    Started : Tuesday, January 17, 2017 11:39:14 AM
    Source – \\deerpark.local\SYSVOL\deerpark.local\scripts\Citywide\
    Dest – C:\Users\ahollister\downloads\Citywide Signature\Signature\

    Files :
    Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30

    ——————————————————————————

    ERROR : Invalid Parameter #3 : "C:\Users\ahollister\AppData\Roaming\microsoft\signatures\"

  • #62044
    Profile photo of random commandline
    random commandline
    Participant

    No need for Invoke-Expression and a double quotation was in the wrong place.

    $source= "\\addomainname\SYSVOL\deerpark.local\scripts\Citywide Signature\"
    $Destination= "$env:APPDATA\microsoft\signatures\"
    
    If (-NOT(test-path "$env:APPDATA\microsoft\signatures\citywide signature.txt")) {
        robocopy $Source $Destination "/r:5","/w:10","/e"
    } Else {Write-Verbose "File exists for $env:USERNAME" -Verbose}
    
    • #62047
      Profile photo of NetAdminTX
      NetAdminTX
      Participant

      using your method, I now see this error...

      Started : Tuesday, January 17, 2017 12:49:48 PM
      Source : \\deerpark.local\SYSVOL\deerpark.local\scripts\Citywide Signature" C:\Users\ahollister\AppData\Roaming\microsoft\signatures\ \r:5,\w:10,\e\
      Dest –
      Files : *.*
      Options : *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30
      ——————————————————————————
      ERROR : No Destination Directory Specified.

  • #62074
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    the best method I found for robocoopy is:

    $source= "\\addomainname\SYSVOL\deerpark.local\scripts\Citywide Signature\"
    $Destination= $env:APPDATA + "\microsoft\signatures\"
    $options= $source, $destination, "/r:5","/w:10","/e"
    
    & robocopy $options
    
    
    • #62083
      Profile photo of NetAdminTX
      NetAdminTX
      Participant

      shows the whole string as the source

      ——————————————————————————-
      ROBOCOPY :: Robust File Copy for Windows
      ——————————————————————————-

      Started : Wednesday, January 18, 2017 9:28:49 AM
      Source : \\deerpark.local\SYSVOL\deerpark.local\scripts\Citywide Signature" C:\Users\ahollister\AppData\Roaming\microsoft\signatures\ \r:5 \w:10 \e\
      Dest –

      Files : *.*

      Options : *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30

      ——————————————————————————

      ERROR : No Destination Directory Specified.

  • #62116
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    I think you forget a comma or a quote between $source and $destination.
    why you don't post command executed, just result ? I haven't a crystal ball 🙂

    PS C:\> $source= 'C:\111'
    PS C:\> $Destination= 'C:\222'
    PS C:\> $options= $source, $Destination, "/r:5", "/w:10", "/e"
    PS C:\> & robocopy $options
    
    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows
    -------------------------------------------------------------------------------
    
      Started : Thu Jan 19 11:06:44 2017
    
       Source : C:\111\
         Dest : C:\222\
    
        Files : *.*
    
      Options : *.* /S /E /COPY:DAT /R:5 /W:10
    
    ------------------------------------------------------------------------------
    
                               3    C:\111\
    100%        New File                 233        disableIPV6.bat
    100%        New File              931933        DynDNS.log
    100%        New File               60617        log
    
    ------------------------------------------------------------------------------
    
                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         1         0         1         0         0         0
       Files :         3         3         0         0         0         0
       Bytes :   969.5 k   969.5 k         0         0         0         0
       Times :   0:00:00   0:00:00                       0:00:00   0:00:00
    
    
       Speed :            62048937 Bytes/sec.
       Speed :            3550.468 MegaBytes/min.
    
       Ended : Thu Jan 19 11:06:44 2017
    
  • #62119
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    NetAdminTX,

    are you running this on Windows 10 build 14393 (Anniversary Update) or 14986? I've done some testing with above code and it looks like the PowerShell engine in both builds and may be later versions too is passing everything as one argument to robocopy instead of separate arguments. It works for me as expected on Windows 7 with PSVersion 5.0.10586.117 which is an older PS engine.

    • #63851
      Profile photo of NetAdminTX
      NetAdminTX
      Participant

      Yep that appears to be the problem.
      I apologize for the late response.

  • #63857
    Profile photo of NetAdminTX
    NetAdminTX
    Participant

    I worked around this problem by using copy-item as opposed to calling robocopy
    I don't know if there is any performance difference, but even if there is it will not make a large difference on 25MB of files.

    thanks for your help everyone.

You must be logged in to reply to this topic.