Copy file from multiple shares to one location v2

Hi EE

I had this question answered by oBdA below but I still need a bit of help .
https://www.experts-exchange.com/questions/29071695/Copy-file-from-multiple-shares-to-one-location.html

Both of those scripts work but the files are being overridden if they have a similar path .

example:
\\Server1\folder\folder2\folder3\SamAccountname1
\\Server1\folder\folder2\folder3\SamAccountname2

In the example above only the file for SamAccountname2 is being saved or overriding the file for SAmaccountName1
Why I am thinking it would be easier of the file names can just be saved as the entire path or just the last 7 characters of the path.

$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

LVL 2
MilesLoganAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
Because you said the folder structure would be "\\Server\Share\SamAccountName"; now it's "\\Server1\folder\folder2\folder3\SamAccountname1".
* What is the exact folder structure? Is "SamAccountName" always the folder containing "logfile.log"?
* Is SamAccountName unique over all servers and shares?
* Is the source server/source share relevant, or are you just interested in the contents?
MilesLoganAuthor Commented:
HI 0BdA

that was my mistake thinking the folder structure was the same for all but its not ...

What is the exact folder structure? Is "SamAccountName" always the folder containing "logfile.log"?
      The structure is different for all unfortunately , the only unique item is the samaccountname at the end of the path,
 Is SamAccountName unique over all servers and shares?
      Yes, this is the only unique item across for all the shares that I need the file from .

Is the source server/source share relevant, or are you just interested in the contents?
    I only care about the file I need to copy from the source .
oBdACommented:
If SamAccountName is the file's parent folder, it's no big deal:
$File = 'logfile.log'
$Destination = 'C:\Temp\Location'
$CopyLog = 'C:\Temp\Location\_LogfileCopy.log'
Get-Content -Path .\shares.txt | ForEach-Object {
	$SamAccountName = $_.Split('\', [StringSplitOptions]::RemoveEmptyEntries)[-1]
	$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

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:
insane ! you are just amazing .. I so appreciate this .
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.