[CmdletBinding()] param( [string]$Path = "$env:SystemRoot\System32\drivers\etc\hosts" ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Файл hosts не найден: $Path" } $entries = New-Object System.Collections.Generic.List[object] $lineNumber = 0 foreach ($line in Get-Content -LiteralPath $Path -ErrorAction Stop) { $lineNumber++ $clean = ($line -split '#', 2)[0].Trim() if (-not $clean) { continue } $parts = $clean -split '\s+' if ($parts.Count -lt 2) { continue } $ip = $parts[0] $hosts = $parts[1..($parts.Count - 1)] -join ', ' $parsed = $null $issue = '' if (-not [System.Net.IPAddress]::TryParse($ip, [ref]$parsed)) { $issue = 'неверный IP' } elseif ($ip -eq '0.0.0.0') { $issue = 'блокировка' } elseif ($ip -eq '127.0.0.1' -and $hosts -notmatch '^localhost(,|$)') { $issue = 'локальная подмена' } $entries.Add([pscustomobject]@{ Line = $lineNumber IP = $ip Hosts = $hosts Issue = $issue }) } $entries | Format-Table -AutoSize Write-Host "Активных строк: $($entries.Count)" $invalid = @($entries | Where-Object { $_.Issue -eq 'неверный IP' }) $blocked = @($entries | Where-Object { $_.Issue -eq 'блокировка' }) $duplicates = @($entries | ForEach-Object { $_.Hosts -split ',\s*' } | Group-Object | Where-Object { $_.Count -gt 1 }) if ($invalid.Count -gt 0) { Write-Warning "Строк с неверным IP: $($invalid.Count)" } if ($blocked.Count -gt 0) { Write-Warning "Строк с блокировкой через 0.0.0.0: $($blocked.Count)" } if ($duplicates.Count -gt 0) { Write-Warning "Повторяющихся имён: $($duplicates.Count)" } $file = Get-Item -LiteralPath $Path Write-Host "Файл изменён: $($file.LastWriteTime.ToString('yyyy-MM-dd HH:mm'))"