Skip to content

Instantly share code, notes, and snippets.

@dpo007
Last active January 27, 2022 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpo007/f8fba4d5d47d57d68c9bc182112fae5b to your computer and use it in GitHub Desktop.
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.
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