Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

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 .
Avatar of oBdA
oBdA

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

Avatar of MilesLogan

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes , that is correct .. same pattern
Thank you so much oBdA..  Happy Holidays !