Copy different files to different machines

I am trying to get a script to work that will copy over a file from a list of files to a number of different machines in a network. I found some scripts that do part of the work, but not everything I would like.

#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

This script will pull a list of computers, but I would also like it to pull a list of files that need to be copied.

Example:

Computer text file

line 1 Workstation1

line 2 Workstation2

File text file

line 1 config1.cfg

line 2 config2.cfg

Thanks!

Answers


Personally I'd rather use the Open File Dialog window for selecting files. As such, I'm tossing in a function to allow it. Allowed some basic filters to be used if desired (usage is -Filter "csv","txt" as an example).

Function Select-File {
Param([switch]$Multi = $False,
[ValidateSet("csv","txt","doc","docx","xls","xlsx")][Array]$Filter)
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{InitialDirectory = [Environment]::GetFolderPath('Desktop')}
If($Multi){$FileBrowser.Multiselect = $true}
If($Filter){$FileFilter=@()
Switch($Filter){
"csv"{$FileFilter+="Comma Seperated Value (*.csv)|*.csv"}
"doc"{$FileFilter+="Older Word Document (*.doc)|*.doc"}
"docx"{$FileFilter+="Word Document (*.docx)|*.docx"}
"xls"{$FileFilter+="Older Excel Document (*.xls)|*.xls"}
"xlsx"{$FileFilter+="Excel Document (*.xlsx)|*.xlsx"}
"txt"{$FileFilter+="Text File (*.txt)|*.txt"}
}$FileFilter = $FileFilter -join "|"
$FileBrowser.Filter = $FileFilter}
[void]$FileBrowser.ShowDialog()
$FileBrowser.FileNames
}

#Load Computer List From File
$Computers = Get-Content (Select-File -Filter "txt")
#Load File Config From File
$Files = Import-CSV (Select-File -Filter "csv")
#Loop through all computers
ForEach($Computer in $Computers){
    #Loop through each file on a per-computer basis
    ForEach($File in $Files){Copy-Item $File.From "\\$computer\c$\$($File.To.TrimStart('C:\'))"}
}

Ok, that assumes that you have a csv for files with 2 columns, From and To being the full path to the file you are copying from, and the full path that you want to copy to. It will remove the C:\ at the beginning of the destination path if it was included in the file.


Need Your Help