# Configuration $swu = "srvfile.jarvis.local\SWU" $ebu = "srvfile.jarvis.local\EBU" $sourcePaths = @( @{Server = "\\$swu"; CredFile = "C:\temp\Credentials\server1_cred.xml"; FileName = "data1.csv"}, @{Server = "\\$ebu"; CredFile = "C:\temp\Credentials\server2_cred.xml"; FileName = "data2.csv"} # @{Server = "\\Server3\Share"; CredFile = "C:\Scripts\Credentials\server3_cred.xml"; FileName = "data3.csv"} ) $localPath = "C:\Temp\CSVFiles" $mergedFile = "C:\Temp\Merged\combined_plates.csv" $destinationPath = "\\srvdc.jarvis.local\output\final_plates.csv" # Create local directories if they don't exist New-Item -ItemType Directory -Path $localPath -Force | Out-Null New-Item -ItemType Directory -Path (Split-Path $mergedFile) -Force | Out-Null # Function to copy files using specific credentials function Copy-FileWithCreds { param($remoteServer, $credentialFile, $fileName, $localPath) try { $cred = Import-Clixml -Path $credentialFile $driveLetter = [guid]::NewGuid().ToString("n").Substring(0, 6) # Map temporary drive New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $remoteServer -Credential $cred -Scope Script -ErrorAction Stop # Copy file Copy-Item -Path "${driveLetter}:\kfz\$fileName" -Destination $localPath -Force -ErrorAction Stop Write-Host "Successfully copied $fileName from $remoteServer" } catch { Write-Warning "Error copying from $remoteServer : $_" } finally { # Cleanup drive if it exists if (Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue) { Remove-PSDrive -Name $driveLetter -Force } } } # Copy all source files foreach ($source in $sourcePaths) { Copy-FileWithCreds -remoteServer $source.Server -credentialFile $source.CredFile -fileName $source.FileName -localPath $localPath } $outputLines = Get-ChildItem -Path $localPath\*.csv | ForEach-Object { Get-Content $_.FullName | Where-Object { $_ -match ";" } } $outputLines | Set-Content -Path $mergedFile -Encoding UTF8 $totalLines = ($outputLines | Measure-Object).Count Write-Host "Successfully merged $totalLines entries from all files" # # Merge CSV files # $combinedData = Get-ChildItem -Path $localPath\*.csv | # ForEach-Object { # if ($_.Name -eq "data1.csv") { # Import-Csv $_.FullName # } # else { # Import-Csv $_.FullName | Select-Object -Skip 1 # } # } # # $combinedData | Export-Csv -Path $mergedFile -NoTypeInformation -Encoding UTF8 # Copy merged file to destination try { $destCred = Import-Clixml -Path "C:\temp\Credentials\destination_cred.xml" $driveLetter = [guid]::NewGuid().ToString("n").Substring(0, 6) New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root (Split-Path $destinationPath -Parent) -Credential $destCred -Scope Script Copy-Item -Path $mergedFile -Destination "${driveLetter}:\" -Force Write-Host "Successfully copied merged file to destination" } catch { Write-Warning "Error copying to destination: $_" } finally { if (Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue) { Remove-PSDrive -Name $driveLetter -Force } } # Optional: Cleanup local files # Remove-Item -Path $localPath\* -Force # Remove-Item -Path $mergedFile -Force