[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string[]]$Path, [string]$OutFile = 'combined.csv', [switch]$Force ) $ErrorActionPreference = 'Stop' $inputFiles = @() foreach ($entry in $Path) { foreach ($part in ($entry -split ',')) { $trimmed = $part.Trim() if ($trimmed.Length -gt 0) { $inputFiles += $trimmed } } } $resolved = @() foreach ($item in $inputFiles) { if ($item -match '[\*\?\[]') { $found = @(Resolve-Path -Path $item -ErrorAction SilentlyContinue | Where-Object { Test-Path -LiteralPath $_.Path -PathType Leaf } | Select-Object -ExpandProperty Path) if ($found.Count -eq 0) { throw "Не найдено файлов: $item" } $resolved += $found } else { if (-not (Test-Path -LiteralPath $item -PathType Leaf)) { throw "Файл не найден: $item" } $resolved += (Resolve-Path -LiteralPath $item).Path } } $resolved = @($resolved | Select-Object -Unique) if ([System.IO.Path]::IsPathRooted($OutFile)) { $outFull = [System.IO.Path]::GetFullPath($OutFile) } else { $outFull = [System.IO.Path]::GetFullPath((Join-Path (Get-Location).Path $OutFile)) } if ($resolved -contains $outFull) { throw "Выходной файл не должен совпадать с одним из входных" } if (Test-Path -LiteralPath $outFull) { if (-not $Force) { throw "Файл уже существует: $outFull. Добавьте -Force для перезаписи" } } $columns = [System.Collections.Generic.List[string]]::new() $rows = [System.Collections.Generic.List[object]]::new() foreach ($file in $resolved) { $items = @(Import-Csv -LiteralPath $file) if ($items.Count -eq 0) { Write-Warning "Пропуск файла без строк: $file" continue } foreach ($row in $items) { foreach ($name in $row.PSObject.Properties.Name) { if (-not $columns.Contains($name)) { [void]$columns.Add($name) } } [void]$rows.Add($row) } } if ($rows.Count -eq 0) { throw "Во входных файлах нет строк данных" } $rows | Select-Object -Property $columns.ToArray() | Export-Csv -LiteralPath $outFull -NoTypeInformation -Encoding UTF8 Write-Host "Файлов: $($resolved.Count)" Write-Host "Строк: $($rows.Count)" Write-Host "Колонок: $($columns.Count)" Write-Host "Результат: $outFull"