Update windoof/movecsv/DataMerger.ps1
This commit is contained in:
parent
73153525df
commit
7a15a56b36
1 changed files with 33 additions and 22 deletions
|
@ -2,14 +2,14 @@
|
|||
$swu = "srvfile.jarvis.local\SWU"
|
||||
$ebu = "srvfile.jarvis.local\EBU"
|
||||
$sourcePaths = @(
|
||||
@{Server = "\\$swu"; CredFile = "C:\Scripts\Credentials\server1_cred.xml"; FileName = "data1.csv"},
|
||||
@{Server = "\\$ebu"; CredFile = "C:\Scripts\Credentials\server2_cred.xml"; FileName = "data2.csv"},
|
||||
@{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 = "\\DestinationServer\Share\final_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
|
||||
|
@ -18,16 +18,16 @@ 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}:\$fileName" -Destination $localPath -Force -ErrorAction Stop
|
||||
Copy-Item -Path "${driveLetter}:\kfz\$fileName" -Destination $localPath -Force -ErrorAction Stop
|
||||
Write-Host "Successfully copied $fileName from $remoteServer"
|
||||
}
|
||||
catch {
|
||||
|
@ -46,24 +46,35 @@ foreach ($source in $sourcePaths) {
|
|||
Copy-FileWithCreds -remoteServer $source.Server -credentialFile $source.CredFile -fileName $source.FileName -localPath $localPath
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
}
|
||||
$outputLines = Get-ChildItem -Path $localPath\*.csv |
|
||||
ForEach-Object {
|
||||
Get-Content $_.FullName | Where-Object { $_ -match ";" }
|
||||
}
|
||||
|
||||
$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
|
||||
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)
|
||||
|
||||
|
||||
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"
|
||||
|
@ -78,5 +89,5 @@ finally {
|
|||
}
|
||||
|
||||
# Optional: Cleanup local files
|
||||
Remove-Item -Path $localPath\* -Force
|
||||
Remove-Item -Path $mergedFile -Force
|
||||
# Remove-Item -Path $localPath\* -Force
|
||||
# Remove-Item -Path $mergedFile -Force
|
Loading…
Add table
Add a link
Reference in a new issue