[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-05
4
Medium Priority
?
708 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:lo_oscar
  • 2
4 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 36919297
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

0
 

Author Comment

by:lo_oscar
ID: 36920087
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}
else
{write-output "$($_.fullname) copied OK to $destination" | out-file -append $logProgress}
}
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 36930714
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
out-file 

Open in new window

0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question