The Multifunctioning DBA

Feb 17 2009   10:42PM GMT

File Copy with Powershell



Posted by: Colin Smith
Tags:
Microsoft Windows
Powershell
Windows
Windows Administration

I have a friend that I work with that has to copy files out to the same 10 servers anytime our developers create a new report. To do this manually he has to log into the UNC of each server, verify that the server does not already have a report with the same name, if it does he needs to rename it, and then copy the file to the server. He thought that he would be able to script this in powershell and asked me for some help along the way. I think that he has a solution but I decided that this is something that may be handy for many of us Admins. So I went ahead and wrote a script that will do what he needs. I decided to add a few extras in as well. Here is what I ended up with.

#Get list of servers to push new files to#

$servers
=
Get-Content
“d:\test2\servers\servers.txt”

#Set where the new files are located and read them in as a variable#

$newfilepath
=
“d:\test2″

$newfiles
=
Get-ChildItem
$newfilepath
|
where {$_.name
-like
“*.txt*”}

#Get Date and Time Information#

$date
=
Get-Date
-format
MM_dd_yyyy

$time
=
get-date
-format
HH:mm:ss

#Test to see if OUT directory and Log file Exist. If not create them#

if (!(test-path
“d:\OUT”))

{

New-Item
-type
directory
“d:\OUT\”

}

if (!(Test-Path
“d:\OUT\rptmove.log”))

{

New-Item
-type
file
“d:\OUT\rptmove.log”

}

$log
=
“d:\OUT\rptmove.log”

#Start Checking each server to see if it has a file with the same name in the destination path#

foreach ($server
in
$servers)

{

Echo
“########################################################################################” >> $log

Echo
“`n########################################################################################” >> $log

Echo
“Working with Server $server” >> $log

# Ping server to make sure it is available for this task#

$pingresult
=
Get-WmiObject
win32_pingstatus
-f
“address=’$Server’”

if($pingresult.statuscode
-eq 0)

{


write-host
$server
is
available
-background
“green”
-foreground
“black”

Echo
“$server is available” >> $log

$prodfolderpath
=
“\\$server\d$\Reports”

$oldfiles
=
Get-ChildItem
$prodfolderpath
|
where{$_.name
-like
“*.txt*”}

foreach ($newfile
in
$newfiles)

{

$found
= 1

$newrpt
=
$newfile.Name

foreach ($oldfile
in
$oldfiles)

{

$oldrpt
=
$oldfile.name

# If the server has a file with the same name, Rename file with datestamp, copy new file to destination#

if ($newrpt
-eq
$oldrpt)

{

Echo
“`nMatch found for $newrpt” >> $log

Echo
“Renaming $oldrpt to $oldrpt.$date” >> $log

Rename-Item
-path
“$prodfolderpath\$oldrpt”
-newName
“$oldrpt.$date”

Echo
“copy $newrpt to $prodfolderpath\newrpt” >> $log

copy-Item
-path
“$newfilepath\$newrpt”
-destination
“$prodfolderpath”

echo
“moving of file $newrpt is complete on $date at $time” >> $log

$found
= 0



}

}

# if server does not have a file with the same name then move the new file to the server. #

if ($found
-eq 1)

{

echo
“`n$newrpt not found in $prodfolderpath” >> $log

copy-Item
-path
“$newfilepath\$newrpt”
-destination
“$prodfolderpath”

echo
“moving of file $newrpt is complete on $date at $time” >> $log

$found
= 0


}

}

}

else

{


write-host
$server
is
NOT
available. Please
check
server
and
try
again
-background
“red”
-foreground
“black”

Echo
“$server is not available for this operation. Please check server and try again!” >> $log

}

Echo
“`nCompleted Working with $server” >> $log

}

Echo
“########################################################################################” >> $log\

So this script fist gets a list of all the servers that it needs to copy the files to. Lucky for me that all the servers keep the reports in the same location. I then get a list of all the file names that I need to upload to the servers. Then I get Date and Time info so that I can use that in renaming files as well as logging actions. Then I start my loop. First I ping the server that I want to work with. If the server is down I echo a notification of this as well as log it. I then move to the next server in the list. If the server is up then I get a listing of all the files that the server has on it in that folder so I can compare them to the files that I need to copy to the server. Then I compare the files. If I find that the server has a file with the same name as a new file then I rename the file on the server using the datestamp so I will know when the file was last used. I also log that action. Then I copy out the new file to the server. If the server does not have a file with the same name as one that I need to upload I upload that file to the server and log that action as well. This will loop thru as many servers as you want with as many files as you want. In this example I am searching only for .txt files but you do not have to do that. Please let me know if you have any questions about this by leaving a comment or by heading over to http://sysadminsmith.com and clicking on Submit a Question to the right.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: