locked
copying multiple files to multiples destinations RRS feed

  • Question

  • hi all

    i have this script:

    
    
    #this file contains the list of destination server that you want copy file/folder to
    
    $computers = Get-Content "C:\ServerList.txt"
    
    #this variable contains the source file/folder you want to copy from
    
    $source = "Path_To_File_Source"
    
    #this variable is the destination that you want to copy to
    
    $destination = Get-Content "Path_To_Target"
    
    #this is the main function for copying file/folder to servers
    
    function main 
       { 
        foreach ($computer in $computers)
            {
    
     copy-item $source -destination "\\$computer\$destination" -Recurse -Force
    
            }
        }    
    main
    
    

    how do i change it that i could copy bulk files instead of one by one.

    i thought to do $files = Get-Content c:\FIlesToCopy.,txt (to read which file to copy)

    but i am not sure how to revise the destination section

    many thanks

    E  

    Tuesday, May 1, 2018 2:14 PM

Answers

  • Hi Bill

    do you mean something like this?

    function main { foreach ($computer in $computers) { foreach ($source in $listOfSources) { foreach ($target in $ListOfTargets) { copy-item $source -destination "\\$computer\$target" -Recurse -Force } } } }

    • Marked as answer by EDolev Wednesday, May 2, 2018 7:02 AM
    Tuesday, May 1, 2018 2:37 PM
  • The following will copy an array of files to each target on each folder.

    foreach ($computer in $computers) {
        foreach($target in $targets){
            Write-Output "$([datetime]::Now) Copying files update to $computer  now" | Out-File -FilePath "$logDir\$logFileName" -Append
            copy-item $listOfSources -destination "\\$computer\$target" -Recurse -Force -Verbose
        }
    }
    


    \_(ツ)_/

    • Marked as answer by EDolev Wednesday, May 2, 2018 7:01 AM
    Wednesday, May 2, 2018 6:45 AM

All replies

  • If you have multiple destinations, you can iterate those with a foreach loop as well. Example:


    foreach ( $source in $listOfSources ) {
      foreach ( $target in $ListOfTargets ) {
        Copy-Item $source $target
      }
    }
    


    -- Bill Stewart [Bill_Stewart]


    Tuesday, May 1, 2018 2:24 PM
  • Hi Bill

    do you mean something like this?

    function main { foreach ($computer in $computers) { foreach ($source in $listOfSources) { foreach ($target in $ListOfTargets) { copy-item $source -destination "\\$computer\$target" -Recurse -Force } } } }

    • Marked as answer by EDolev Wednesday, May 2, 2018 7:02 AM
    Tuesday, May 1, 2018 2:37 PM
  • Something like that. You can test it by adding the -WhatIf parameter to Copy-Item to test it out. Or, you can also output a string that contains the computer name, source, and target, to make sure your loops are correct.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 1, 2018 2:40 PM
  • Hi Bill

    I have tried the main function and it works well.

    the only thing, the foreach make the file copy 5 times.

    see as follow:

    function main 
    
       { 
    
        foreach ($computer in $computers) {
    
            foreach ($source in $listOfSources) {
    
             foreach ($target in $ListOfTargets) {
                
                Write-Output "$([datetime]::Now) Copying file update to $computer $target now" | Out-File -FilePath "$logDir\$logFileName" -Append
    
     copy-item $source -destination "\\$computer\$target" -Recurse -Force
              }
              }
            }
        }    
    main

    i am trying to think how to make it stop after the first run.

    any ideas?

    many thanks

    E

    Wednesday, May 2, 2018 6:38 AM
  • The following will copy an array of files to each target on each folder.

    foreach ($computer in $computers) {
        foreach($target in $targets){
            Write-Output "$([datetime]::Now) Copying files update to $computer  now" | Out-File -FilePath "$logDir\$logFileName" -Append
            copy-item $listOfSources -destination "\\$computer\$target" -Recurse -Force -Verbose
        }
    }
    


    \_(ツ)_/

    • Marked as answer by EDolev Wednesday, May 2, 2018 7:01 AM
    Wednesday, May 2, 2018 6:45 AM
  • Thank you Both very much.

    Works perfect

    Many Thanks to you both

    E

    Wednesday, May 2, 2018 7:02 AM