[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$Column = 'phone', [string]$OutFile = '', [string]$DefaultCountry = '7', [switch]$KeepOriginal, [switch]$Force ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Файл не найден: $Path" } $resolved = (Resolve-Path -LiteralPath $Path).Path if ([string]::IsNullOrWhiteSpace($OutFile)) { $directory = [System.IO.Path]::GetDirectoryName($resolved) $stem = [System.IO.Path]::GetFileNameWithoutExtension($resolved) $OutFile = Join-Path $directory ($stem + '_phones.csv') } 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" } } $country = $DefaultCountry -replace '\D', '' if ($country.Length -lt 1 -or $country.Length -gt 3) { throw "Код страны должен содержать 1-3 цифры" } $rows = @(Import-Csv -LiteralPath $resolved) if ($rows.Count -eq 0) { throw "В CSV нет строк данных" } if (-not ($rows[0].PSObject.Properties.Name -contains $Column)) { throw "Колонка не найдена: $Column" } $changed = 0 $invalid = 0 foreach ($row in $rows) { $raw = [string]$row.$Column $digits = $raw -replace '[^\d]', '' if ([string]::IsNullOrWhiteSpace($digits)) { $normalized = '' if ($raw.Trim().Length -gt 0) { $invalid += 1 } } else { if ($digits.StartsWith('00')) { $digits = $digits.Substring(2) } if ($digits.Length -eq 10) { $digits = $country + $digits } elseif ($country -eq '7' -and $digits.Length -eq 11 -and $digits.StartsWith('8')) { $digits = '7' + $digits.Substring(1) } if ($digits.Length -lt 10 -or $digits.Length -gt 15) { $normalized = $raw.Trim() $invalid += 1 } else { $normalized = '+' + $digits } } if ($KeepOriginal) { $row | Add-Member -NotePropertyName "${Column}Original" -NotePropertyValue $raw -Force } if ($normalized -ne $raw.Trim()) { $changed += 1 } $row.$Column = $normalized } $rows | Export-Csv -LiteralPath $outFull -NoTypeInformation -Encoding UTF8 Write-Host "Строк: $($rows.Count)" Write-Host "Изменено: $changed" Write-Host "Некорректных: $invalid" Write-Host "Результат: $outFull"