none
Powershell - Copy a list of files to multiple servers and backup exisiting files

    Question

  • Hello..

    I would like to have assistance from Powershell gurus..

    My scenario is; I want to automate a task to copy some .dll files from one location to multiple servers but before copying those files over to those servers; I'd like to take a backup for those same files which will be overwritten on to a local server where these .dll files will be copied from

    My preference would be that the script should read 1 file (.txt or .csv) which will contain the name of those files that need to be copied over and get backedup and then read another file which will contain the list of servers.

    example

    File to copy = filename.dll (this filename.dll should be read from a .txt or .csv file which could contain more than 1 file)

    Server to copy to = server1, server2, server3 (this should be read from a .csv file)

    From the local server; Read filename(s).dll from the file, the servername from the server file and before copying the new files over, copy those exact same files from the shared folders on those servers and paste them on the local server by creating a folder as the servername and paste the file in there.

    Then copy the new files over to those servers from the list and overwrite the existing files.

    I really appreciate your time and effort in this

    Thanks

    Wednesday, June 06, 2012 4:59 PM

Answers

  • Hi,

    This is a very simple powershell script to transfer a file to a list of computes contained in a text file.

    #Point the script to the text file
    $Computers = Read-Host "Enter Location Of TXT File"
    # sets the varible for the file location ei c:\temp\ThisFile.exe
    $Source = Read-Host "Enter File Source"
    # sets the varible for the file destination
    $Destination = Read-Host "Enter File destination (windows\temp)"
    # displays the computer names on screen
    Get-Content $Computers | foreach {Copy-Item $Source -Destination \\$_\c$\$Destination

    Or:

    $a = Get-Content "D:\Scripting\Srvlist.txt" foreach ($i in $a) {$files= get-content "D:\Scripting\filelist.txt"

    foreach ($file in $files)

    {Copy-Item $file -Destination \\$i\C$\temp -force}

    }

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Tuesday, June 12, 2012 2:42 AM
    Moderator

All replies

  • Hi,

    This is a very simple powershell script to transfer a file to a list of computes contained in a text file.

    #Point the script to the text file
    $Computers = Read-Host "Enter Location Of TXT File"
    # sets the varible for the file location ei c:\temp\ThisFile.exe
    $Source = Read-Host "Enter File Source"
    # sets the varible for the file destination
    $Destination = Read-Host "Enter File destination (windows\temp)"
    # displays the computer names on screen
    Get-Content $Computers | foreach {Copy-Item $Source -Destination \\$_\c$\$Destination

    Or:

    $a = Get-Content "D:\Scripting\Srvlist.txt" foreach ($i in $a) {$files= get-content "D:\Scripting\filelist.txt"

    foreach ($file in $files)

    {Copy-Item $file -Destination \\$i\C$\temp -force}

    }

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Tuesday, June 12, 2012 2:42 AM
    Moderator
  • Hi ,

    There is a way to get log from the second script? I mean, log which files were copied?

    Friday, September 12, 2014 3:08 PM
  • Hi ,

    There is a way to get log from the second script? I mean, log which files were copied?

    You can use the -PassThru switch:

    http://ss64.com/ps/copy-item.html

    If you have additional questions, please start your own thread.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Friday, September 12, 2014 3:13 PM
  • thank You! And sorry about that.
    Friday, September 12, 2014 5:33 PM
  • Thanks this helped a lot!

    But is there a way to read a text file which contains the source paths of multiple files which you want to copy to multiple computers?

    Using the same process which allows you to put multiple computers in a text file. 

    Thanks in advance, 

    Tom 

    Friday, October 24, 2014 2:29 PM
  • Hello,

    How about copying MULTIPLE FILES to MULTIPLE SERVERS.

    Above script is all about copying single file to all servers.

    In my scenario, i do not want to copy a folder instead i would like to pickup few from it. i.e more than 1.

    regards,

    Dharanesh,


    • Edited by Dharanesh Wednesday, February 25, 2015 6:00 AM
    Wednesday, February 25, 2015 5:59 AM
  • Hello,

    How about copying MULTIPLE FILES to MULTIPLE SERVERS.

    Above script is all about copying single file to all servers.

    In my scenario, i do not want to copy a folder instead i would like to pickup few from it. i.e more than 1.

    regards,

    Dharanesh,


    Please start your own thread. You'll need to post your code and your errors.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Wednesday, February 25, 2015 1:27 PM