addet windoof

This commit is contained in:
pika 2025-04-14 12:54:23 +02:00
parent cc8ffcfcc2
commit e020df5324
2 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,80 @@
# Configuration
$sourcePaths = @(
@{Server = "\\Server1\Share"; CredFile = "C:\Scripts\Credentials\server1_cred.xml"; FileName = "data1.csv"},
@{Server = "\\Server2\Share"; CredFile = "C:\Scripts\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"
# 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}:\$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
}
# 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"
$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

21
windoof/movecsv/README.md Normal file
View file

@ -0,0 +1,21 @@
## setup
You need to have the credentials present before running the script
```ps1
# Save credentials for source servers
$cred1 = Get-Credential -Message "Enter credentials for Server1"
$cred1 | Export-Clixml -Path "C:\Scripts\Credentials\server1_cred.xml"
$cred2 = Get-Credential -Message "Enter credentials for Server2"
$cred2 | Export-Clixml -Path "C:\Scripts\Credentials\server2_cred.xml"
$cred3 = Get-Credential -Message "Enter credentials for Server3"
$cred3 | Export-Clixml -Path "C:\Scripts\Credentials\server3_cred.xml"
# Save credentials for destination server (if needed)
$destCred = Get-Credential -Message "Enter credentials for Destination Server"
$destCred | Export-Clixml -Path "C:\Scripts\Credentials\destination_cred.xml"
```