Private/Utils/Import-CsvWithFallback.ps1
|
function Import-CsvWithFallback { <# .SYNOPSIS Import CSV with automatic delimiter detection. .DESCRIPTION Attempts to import CSV using comma delimiter first, falls back to semicolon if initial import fails. Useful for handling CSV files from different sources. .PARAMETER Path Path to the CSV file to import. .EXAMPLE $data = Import-CsvWithFallback -Path "C:\data\policies.csv" .OUTPUTS Array of PSCustomObject representing CSV rows #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path ) if (-not (Test-Path $Path)) { throw "CSV file not found: $Path" } try { Import-Csv -Path $Path -Delimiter ',' -ErrorAction Stop } catch { Write-Warning "Failed to parse with comma delimiter, trying semicolon for: $Path" Import-Csv -Path $Path -Delimiter ';' } } |