0

I would like to parallel copy using robocopy and returning log data when it is done.

if i were to use invoke-command server1,server2,server3 { robocopy }, it spits out the logs as it is copying for $1, $2, $3. this getting very confusing and hard to read. Is there a way to have the log or progress returns when copying is done?

for example:

server3 done.. display all copied files
server1 done.. display all copied files
server2 done.. display all copied files

instead of

$3 display copied files
server1 display copied files
server2 display copied files
server2 display copied files
server1 display copied files
server2 display copied files

I know i could use runspace to multi threads and check completion for handle to display the data (which i am currently using). but i would like to know if it is possible to do the same with invoke-command and it is easier :).

thanks

SimpleBeat
  • 747
  • 1
  • 10
  • 20
  • 2
    Um, use the `-AsJob` parameter, and then capture the jobs that it creates when you do that, and use `Wait-Job` to wait for them to complete, and `Get-Job` to return the results. – TheMadTechnician Apr 28 '17 at 18:51

1 Answers1

0

To expand on my comment, you could create your desired action as a scriptblock, use the -AsJob parameter to make background jobs for the copy actions, then wait for the jobs to finish and output the results when they're done.

$SB = {
    & robocopy \\FileServer\Share\KittenPictures C:\webroot\MySite\Images *.*
}
$Servers = ('ServerA','ServerB','ServerC')
$Jobs = Invoke-Command -ScriptBlock $SB -ComputerName $Servers -AsJob
While($Jobs.JobStateInfo -Contains Running){
    $Jobs | Wait-Job -Any
    Get-Job -State Completed | ForEach{
        $_ | Receive-Job
        $_ | Remove-Job
    }
}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • awesome it works. i had to modify it a bit. While($Jobs.State -Contains "Running"){ $Jobs | Wait-Job -Any if ($jobs.State -contains "Completed") { Receive-Job -Job $jobs Remove-Job -Job $jobs } } As i have never used jobs before, does it have any performance concerns? like resources? if i were to have more than 100+ servers or doing something that will take hours? thanks for the help – user7938134 Apr 29 '17 at 02:43
  • Actually the above runs the jobs on the remote servers, so it's actually quite effective at running the commands on multiple servers. If you try and run too many jobs on the local server I suppose it could cause some concerns about resources, but remote jobs should be relatively ok. – TheMadTechnician May 01 '17 at 17:09