none
Robocoy inside invoke command let RRS feed

  • Question

  • Hi,

    I want to use Robocopy inside invoke-commandlet

    $passwordFile = "C:\share\pas.txt"
    $username = "username"
    
    # First time create password file
    if (! (Test-Path $passwordFile))
    {
      Read-Host -AsSecureString | convertfrom-securestring | out-file $passwordFile
    }
    
    $password = cat $passwordFile | convertto-securestring
    $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
    $server = "servername"
    Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
    
    $server = "servername"
    $source = "SourcePath"
    # The destination location you want the file/folder(s) to be copied to
    
    $destination = "\\SharedPathOnMyComputer"  
    
     $fileLIst = "4-24.csv"
    
    $robocopyOptions = '/r:5', '/w:15', '/MT:1500'
    $cmd = 'c:\windows\system32\robocopy.exe'
    $Arguments = $source + $destination + $fileLIst +$robocopyOptions
    $Arguments
    & $cmd $source  $destination 
    }

    But it throws this error:

    -------------------------------------------------------------------------------

       ROBOCOPY     ::     Robust File Copy for Windows                             

    -------------------------------------------------------------------------------

      Started : 25 April 2018 00:40:54

       Source : SourcePath" DestinationShare

         Dest -

        Files : *.*

       

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

    ------------------------------------------------------------------------------

    ERROR : No Destination Directory Specified.

    Any clue!!

    Simple robopy command like this


    Simple robopy command like this


    #Start robocopy -args "$source $destination $fileLIst $robocopyOptions" also throw same errorI tried with other way as well but no luck


    Regards
    Rajaniesh

    • Moved by Bill_Stewart Friday, July 27, 2018 6:36 PM This is not "debug/fix/rewrite my script for me" forum
    Wednesday, April 25, 2018 7:49 PM

All replies

  • You cannot remotely connect to a third server.  It is not allowed for security reasons.

    Look up "second hop restriction".


    \_(ツ)_/

    Wednesday, April 25, 2018 8:36 PM
  • Using RoboCopy for one file is unnecessary unless that file is many gigabytes.


    \_(ツ)_/

    Wednesday, April 25, 2018 8:38 PM
  • Guys,

    My puepose of running robocopy was to create logparser logs on remote servers and copy them to my local machine. I am ok if you have any other mechanism like copy-item etc. But I tried it as well and it does not works?

    Regards
    Rajaniesh

    Thursday, April 26, 2018 10:20 PM
  • If you are an Admin then you don't need Invoke-Command.  Just copy the los

    Copy-Item \\server\c$\folder\logfile c:\temp

    This will copy the remote file to the local system folder.


    \_(ツ)_/

    Thursday, April 26, 2018 11:05 PM
  • I am using service account to log to the remote server so If I have to run this command under different credential without calling separate Powershell instance under service account credentials what are my options?

    Also I need to copy to share in the destination server not on the same server Copy-Item \\server\c$\folder\logfile \\ServerName\FolderName

    Friday, April 27, 2018 10:02 PM
  • There are no options.  Run the task under credentials that have correct access and don't use remoting.

    Copy-Item c:\folder\logfile \\RemoteServer\FolderName

    Basic Windows.  There is no trick here.  All Windows commands can use UNC paths.


    \_(ツ)_/

    Friday, April 27, 2018 11:17 PM
  • Did you mean not to use invoke commandlet? How to run copy command under credentials? Did you mean to use credential switch  of copy-item command? If I need to copy the file kept in c:\folder on server name server1 to a share name share1\sharefolder I am using this command

     Copy-Item \\server\c$\folder\fileName \\shareName\FolderName -credential $credential

    but it is throwing error

     Regards
    Rajaniesh
    Friday, April 27, 2018 11:27 PM
  • Do not use Invoke-Command with UNC paths.  The "Credential" parameter of Copy-Item is mot implemented for the FileSystem provider.  Read the full help to see about this.

    If you need to constantly copy new files than use the "/MIR" parameter of Robocopy.  Run RC as a task when Windows starts so that it is always running.


    \_(ツ)_/

    Friday, April 27, 2018 11:50 PM
  • Hi,

    I was able to make it work

     $destination = "\\Server1\Share"  
    $servers = "Server2" 
    $fileLIst = "4-24.csv"
     
     $source = "c$\Program Files (x86)\Log Parser 2.2\" +$fileLIst
     Copy-Item \\$servers\$source   $destination -Verbose

     But when I run this script I need to run it while opening the PowerShell script with the specific credential for Target server. Can I use stored credentials and run it successfully. So to solve the real issue I have to create two script one for collecting the logs and another one for copying the items(which is this one pasted above)

    Regards
    Rajaniesh


    Tuesday, May 1, 2018 10:03 PM
  • Start-Process powershell -arumentlist '-nologo -File myscript.ps1' -Credentials $creds

    help start-Process -online


    \_(ツ)_/

    Tuesday, May 1, 2018 10:10 PM
  • Hi,

    I was able to run it by using this command

    Start-Process powershell.exe -ArgumentList  '-nologo -File c:\folderPath\Script.ps1' -Credential $credential 
    

    and it does not show any error which means it ran successfully but I do not see the output of copy into the folder.

    Regards

    Rajaniesh

    Wednesday, May 2, 2018 7:12 PM