Last active
January 27, 2022 21:32
-
-
Save dpo007/f8fba4d5d47d57d68c9bc182112fae5b to your computer and use it in GitHub Desktop.
PowerShell :: Test-ForPort function - Tests network port for X retires, or until it answers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-ForPort { | |
param ( | |
[Parameter(Mandatory)] | |
[string]$Server, | |
[Parameter(Mandatory)] | |
[int]$Port, | |
[int]$RetryCount = 3, | |
[switch]$Quiet | |
) | |
$answered = $false | |
if (!$Quiet) { | |
$infoLevel = "Quiet" | |
} else { | |
$infoLevel = "Detailed" | |
} | |
while ((!$answered) -and ($RetryCount -gt 0)) { | |
$answered = (Test-NetConnection $Server -port $Port).TcpTestSucceeded | |
$RetryCount -= 1 | |
} | |
if ($RetryCount -lt 1) { | |
if (!$Quiet) { | |
Write-Host 'Timed out.' | |
} | |
return $false | |
} | |
if (!$Quiet) { | |
Write-Host 'Answered.' | |
} | |
return $true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment