Copy file from multiple shares to one location

Does anyone have a script to share that will do something like this ?

I need to copy the same file from multiple shares to one location ..  I can copy the file and rename it , but this is on 100s of shares and I dont have the knowledge to do it in  a mass scale .

basically I need the script to look on a text file for the path to the shares and copy logfile.log to one location . Issue is that since the file is the same , I need it to be renamed when copied so its not over ridden . I dont care about the file name that its copied to .. a number at the end will work .
LVL 2
MilesLoganAsked:
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.

oBdACommented:
This will add the server name and the share name before the file name. It'll write a log file as well.
$File = 'logfile.log'
$Destination = 'C:\Temp\Location'
$CopyLog = 'C:\Temp\Location\_LogfileCopy.log'
Get-Content -Path .\shares.txt | ForEach-Object {
	$Result = $_ | Select-Object -Property @{n='Source'; e={$_}}, @{n='Error'; e={}}
	$Server, $Share = $_.Split('\', [StringSplitOptions]::RemoveEmptyEntries)[0..1]
	Try {
		Copy-Item -Path "$($_)\$($File)" -Destination "$($Destination)\$($Server)_$($Share)_$($File)" -ErrorAction Stop
	} Catch {
		$Result.Error = $_.Exception.Message
	}
	$Result
} | Export-Csv -NoTypeInformation -Path $CopyLog

Open in new window

0
MilesLoganAuthor Commented:
Hi oBdA

This works great  only , issue I ran into is if there is two shares with the same path , it will only copy one file.

This works:
\\Share\folder\User1
\\Share\folder99\User2

This does not:
\\Share\folder\User1
\\Share\folder\User2


would it be too much for you to modify so the output is the samaccountname ? if I have the input file be ..
Column A ,               Column B
SamAccountName,Path

Outputfile wold be the SamAccountName for each file .. if that is too much, no worries .. I can deal with the file names as is ..

If you can get past this ..
This does not:
\\Share\folder\User1\LogFile.log
\\Share\folder\User2\LogFile.log
0
oBdACommented:
So the paths all follow the pattern "\\Server\Share\SamAccountName", and the SamAccountName is unique across all servers and shares?
$File = 'logfile.log'
$Destination = 'C:\Temp\Location'
$CopyLog = 'C:\Temp\Location\_LogfileCopy.log'
Get-Content -Path .\shares.txt | ForEach-Object {
	$Server, $Share, $SamAccountName = $_.Split('\', [StringSplitOptions]::RemoveEmptyEntries)[0..2]
	$Result = $_ | Select-Object -Property @{n='SamAccountName'; e={$SamAccountName}}, @{n='Path'; e={$_}}, Error
	Try {
		Copy-Item -Path "$($_)\$($File)" -Destination "$($Destination)\$($SamAccountName)_$($File)" -ErrorAction Stop
	} Catch {
		$Result.Error = $_.Exception.Message
	}
	$Result
} | Export-Csv -NoTypeInformation -Path $CopyLog

Open in new window

0

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
MilesLoganAuthor Commented:
Yes , that is correct .. same pattern
0
MilesLoganAuthor Commented:
Thank you so much oBdA..  Happy Holidays !
0
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
Powershell

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.