[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$OutFile = '', [ValidateRange(0, 1000000)] [int]$MaxRows = 0, [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Файл не найден: $Path" } $resolved = (Resolve-Path -LiteralPath $Path).Path if ([string]::IsNullOrWhiteSpace($OutFile)) { $OutFile = [System.IO.Path]::ChangeExtension($resolved, '.md') } 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 -eq $outFull) { throw "Выходной файл не должен совпадать с исходным" } if (Test-Path -LiteralPath $outFull) { if (-not $Force) { throw "Файл уже существует: $outFull. Добавьте -Force" } } $rows = @(Import-Csv -LiteralPath $resolved) if ($rows.Count -eq 0) { throw "В CSV нет строк данных" } $columns = @($rows[0].PSObject.Properties.Name) if ($MaxRows -gt 0) { $selected = @($rows | Select-Object -First $MaxRows) } else { $selected = $rows } function Escape-Cell { param([string]$Value) if ($null -eq $Value) { return '' } $text = [string]$Value $text = $text -replace '\|', '\|' $text = $text -replace '\r?\n', '
' return $text } $builder = New-Object System.Text.StringBuilder [void]$builder.AppendLine('| ' + (($columns | ForEach-Object { Escape-Cell $_ }) -join ' | ') + ' |') [void]$builder.AppendLine('| ' + (($columns | ForEach-Object { '---' }) -join ' | ') + ' |') foreach ($row in $selected) { $cells = foreach ($column in $columns) { Escape-Cell ([string]$row.$column) } [void]$builder.AppendLine('| ' + ($cells -join ' | ') + ' |') } $text = $builder.ToString() $encoding = New-Object System.Text.UTF8Encoding($false) [System.IO.File]::WriteAllText($outFull, $text, $encoding) Write-Host "Строк: $($selected.Count) из $($rows.Count)" Write-Host "Колонок: $($columns.Count)" Write-Host "Результат: $outFull"