Param( [Parameter(Mandatory = $true, Position = 0)] [string]$Path, # Wenn angegeben, speichert die Auswahl hier als CSV. [string]$OutFile, # Wenn gesetzt (oder OutFile leer), kopiert die Auswahl in die Zwischenablage. [switch]$ToClipboard ) # --- Vorbereitungen / Checks --- # Für MessageBoxen (Fehler/Info) Add-Type -AssemblyName System.Windows.Forms | Out-Null if (-not (Test-Path -LiteralPath $Path)) { [System.Windows.Forms.MessageBox]::Show("Datei nicht gefunden:`n$Path","Fehler",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null exit 1 } try { $resolved = (Resolve-Path -LiteralPath $Path).Path } catch { [System.Windows.Forms.MessageBox]::Show("Pfad konnte nicht aufgelöst werden:`n$Path`n$($_.Exception.Message)","Fehler",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null exit 1 } # --- Delimiter rudimentär erkennen (',' oder ';') --- $delimiter = ',' try { $firstLine = [System.IO.File]::ReadLines($resolved) | Select-Object -First 1 if ($firstLine -and ($firstLine.Contains(';')) -and (-not ($firstLine.Contains(',') -and -not $firstLine.Contains(';')))) { $delimiter = ';' } } catch { # Falls irgendwas schiefgeht: bei ',' bleiben } # --- CSV einlesen --- try { $data = Import-Csv -LiteralPath $resolved -Delimiter $delimiter } catch { [System.Windows.Forms.MessageBox]::Show("Import-Csv fehlgeschlagen:`n$($_.Exception.Message)","Fehler",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null exit 2 } # --- Auswahl im OGV ermöglichen --- $title = "CSV auswählen – " + [System.IO.Path]::GetFileName($resolved) + " (OK = Auswahl übernehmen)" $selected = $data | Out-GridView -Title $title -PassThru if (-not $selected) { # Benutzer hat OGV geschlossen oder nichts gewählt exit 0 } # Standard: Wenn kein OutFile angegeben ist, kopieren wir in die Zwischenablage. if (-not $OutFile) { $ToClipboard = $true } # --- Ausgabe behandeln --- if ($OutFile) { try { $selected | Export-Csv -Delimiter $delimiter -NoTypeInformation -LiteralPath $OutFile -Force [System.Windows.Forms.MessageBox]::Show("Auswahl gespeichert in:`n$OutFile","Fertig",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null } catch { [System.Windows.Forms.MessageBox]::Show("Export-Csv fehlgeschlagen:`n$($_.Exception.Message)","Fehler",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null exit 3 } } elseif ($ToClipboard) { try { # In Textform in die Zwischenablage $selected | Set-Clipboard [System.Windows.Forms.MessageBox]::Show("Ausgewählte Zeilen wurden in die Zwischenablage kopiert.","Fertig",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null } catch { [System.Windows.Forms.MessageBox]::Show("Konnte nicht in die Zwischenablage schreiben:`n$($_.Exception.Message)","Fehler",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null exit 4 } }