Powershell script to copy mutiple files in a folder to remote severs and validate the file was replaced successfully

I'm new to powershell and would like to build a script that will accomplish the following:

1. Replace files on multiple servers with files in a file share.
2. After copy, verify the the version of replaced files.

Can someone show me some templates or point me to right direction? Thanks in advance for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

loop through a list of server names, then for each use "net use" command to map a drive, and then use copy-item to copy the files from your share
after copying delete the mapped drive

$cred = Get-Credential()
$networkCred = $cred.GetNetworkCredential()
net use \\servername\test\ $networkCred.Password /USER:$networkCred.UserName
Copy-Item -Path C:\Test\ -Destination \\servername\test\ -Credential $cred
net use \\server\test\ /delete

Open in new window

lo_oscarAuthor Commented:
how do I add multiple destinations in this script?

$source = 'C:\folder1\'
$destination = '\\server\Folder1\'
$logProgress = 'c:\temp\copyLog.txt'

get-childitem $source -recurse | foreach {
copy $_.fullname $destination -Force -Recurse -errorAction silentlyContinue
if($? -eq $false){echo "$($_.fullname) did not copy ok to $destination" | out-file -append $logProgress}
{write-output "$($_.fullname) copied OK to $destination" | out-file -append $logProgress}
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I assume you do not need the credentials stuff as shown by YZlat, and then you do not need the net use either.

Your code would
a) go thru the complete folder structure,
b) and start copying this structure each time
so you would have too many copy operations doing exactly the same.

You already allow copy-item to copy the complete tree. You just need to apply that to more than one destination.
$source = 'C:\folder1\'
$destination = '\\server\Folder1\','\\server2\Folder1'
$logProgress = 'c:\temp\copyLog.txt'

$destination | % {
  write-output "=====`nCopy to: $_`n=====`n"
  copy-item $_.fullname %_ -Force -Recurse -errorAction Continue
} | out-file -append $logProgress

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.