[CmdletBinding()] param( [ValidateSet('127.0.0.1', 'localhost', '::1')] [string]$HostName = '127.0.0.1', [int[]]$Port = @(22, 80, 443, 3000, 5432, 6379, 8000, 8080), [ValidateRange(50, 5000)] [int]$TimeoutMs = 500 ) $ErrorActionPreference = 'Stop' function Test-TcpPort { param( [string]$Target, [int]$Port, [int]$TimeoutMs ) $client = New-Object System.Net.Sockets.TcpClient try { $async = $client.BeginConnect($Target, $Port, $null, $null) if (-not $async.AsyncWaitHandle.WaitOne($TimeoutMs)) { return 'timeout' } $client.EndConnect($async) return 'open' } catch [System.Net.Sockets.SocketException] { return 'closed' } catch { return 'error' } finally { $client.Close() } } $results = foreach ($item in $Port) { if ($item -lt 1 -or $item -gt 65535) { Write-Warning "Пропуск неверного порта: $item" continue } [pscustomobject]@{ Host = $HostName Port = $item State = Test-TcpPort -Target $HostName -Port $item -TimeoutMs $TimeoutMs } } $results | Format-Table -AutoSize $open = @($results | Where-Object { $_.State -eq 'open' }) Write-Host "Открыто: $($open.Count) из $($results.Count)" if ($open.Count -eq 0) { exit 1 }