Update windoof/movecsv/DataMerger.ps1

This commit is contained in:
pika 2025-04-14 13:45:27 +02:00
parent 73153525df
commit 7a15a56b36

View file

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