Parallel copy of files on multiple servers

Sam White 26 Reputation points
2024-06-13T12:30:07.6566667+00:00

I want to copy all the files in a folder called test from ServerA to multiple Servers. I am using command like below, however it is not copying the files:

Executing the below script from ServerA

$serverlist = New-PSSession -ComputerName ( Get-Content C:\servers.txt)

Invoke-Command -ComputerName $serverlist -ScriptBlock {

Copy-item \\ServerA.constoso.local\C$\Temp\Test\* -Destination C:\Temp\Test\ -Recurse -Force

}

However it does not copy the files and does not return any error also. How to copy files to multiple computers at the same time?

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,582 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,346 Reputation points
    2024-06-14T15:38:41.21+00:00

    You are running into the double hop problem.

    https://www.bing.com/search?pglt=41&q=invoke-command+double+hop

    Your credentials work when connecting from server1 to server2, but on server2 you can't connect to server3 even if it is server1.

    One solution is to use a common local account that is a member of the administrators' group on all machines.

    cls
    $computers = @("Test10","rio")
    $src =  '\\{0}\c$\temp' -f $env:computername
    $sb = {
         "{0} {1} Begin." -f (get-date), $env:computername 
         "{0} {1} Source folder is {2}" -f  (get-date), $env:computername, $using:src
         $bdrive = new-psdrive -Name 'z' -PSProvider FileSystem -Root $using:src  -Credential $using:credential
         $Files = Get-ChildItem z:\
         "{0} {1} We found {2} files." -f (get-date), $env:computername, ($Files).Count
         Remove-PSDrive -Name 'z'
         "{0} {1} End." -f (get-date), $env:computername 
    }
    $User = ".\admin"
    $PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    Invoke-Command -ComputerName $computers -ScriptBlock $sb -Credential $Credential 
    

    User's image

    I did add a Start-Sleep for one machine to test to see if the commands were executed one at a time, but it appears that they run concurrently. I have limited machines to test with.

    I don't have an Active Directory environment to test with, but if you do you can try using CredSSP.

    https://4sysops.com/archives/using-credssp-for-second-hop-powershell-remoting/

    The example that you posted is a "pull" transfer where Invoke-command runs a Copy-Item on the destination server to pull the files from the source server via the C$ share. You can also copy the files where you "push" transfer from the source server to the destination server over the PSSession.

    See Example 6 in this link.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-5.1

    I did not test this method, so I don't know if it processes one server at a time or overlaps. If it's one at a time, you can try using a Powershell Job to have multiple copies running at the same time.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job?view=powershell-5.1


2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 46,811 Reputation points
    2024-06-13T15:37:05.59+00:00

    The -ComputerName parameter will accept a list of computer names where each name is a string.

    Invoke-Command -ComputerName (Get-Content C:\servers.txt) -ScriptBlock {
        Copy-item \\ServerA.constoso.local\C$\Temp\Test\* -Destination C:\Temp\Test\ -Recurse -Force
    }
    

    If you want to use those PSSEssions you have to use a different parameter (-Session):

    $sessions = New-PSSession -ComputerName (Get-Content C:\servers.txt)
    Invoke-Command -Session $sessions -ScriptBlock {
        Copy-item \\ServerA.constoso.local\C$\Temp\Test\* -Destination C:\Temp\Test\ -Recurse -Force
    }
    

  2. Olaf Helper 45,096 Reputation points
    2024-06-13T15:49:00.3566667+00:00

    Invoke-Command -ComputerName $serverlist

    The parameter -ComputerName expect one computer name, not a list of several names.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.