# Define an array with the server shares to look ing
$server_shares = @("\\server1\share1","\\server2\share2")
# Local repository to collect the files into
$local_repo = "$env:USERNAME\Documents\Repo"
$pattern = "*FILENAME*.txt*"
$map_drive = ""
# Create the repo if it doesn't exist
If (-not (Test-Path -Path $local_repo))
{
New-Item -Path $local_repo -ItemType Directory
}
# D - Z
# Find an unused drive letter
$char_index = 68..91
foreach($index in $char_index)
{
$letter = [char]$index
if (-not (Test-Path "$($letter):\"))
{
$map_drive = "$($letter)"
break
}
}
# Connect to each share and find files to copy
foreach($server in $server_shares)
{
$files = @()
New-PSDrive -Name $letter -PSProvider FileSystem -Root $server
$files += @((Get-ChildItem -Path "$($letter):\" -Filter $pattern -Recurse))
foreach($file in $files)
{
Copy-Item -Path $file.FullName -Destination $local_repo
}
Start-Sleep -Seconds 2
Remove-PSDrive $letter
Start-Sleep -Seconds 2
}