Skip to content

Instantly share code, notes, and snippets.

@felipepoliveira
Created February 17, 2022 12:25
Show Gist options
  • Save felipepoliveira/5bc0454e03892a7616a68e1900f72128 to your computer and use it in GitHub Desktop.
Save felipepoliveira/5bc0454e03892a7616a68e1900f72128 to your computer and use it in GitHub Desktop.
## Include on the array the services (service name and port on sequence) that you want to check.
$externalServicesNamesAndPorts = @('MySQL', 3306, 'MongoDB', 27017, 'Elastic Search', 9200, 'Apache Zookeeper', 2181, 'Kafka', 9092)
function Write-ColorOutput($ForegroundColor)
{
# save the current color
$fc = $host.UI.RawUI.ForegroundColor
# set the new color
$host.UI.RawUI.ForegroundColor = $ForegroundColor
# output
if ($args) {
Write-Output $args
}
else {
$input | Write-Output
}
# restore the original color
$host.UI.RawUI.ForegroundColor = $fc
}
for ($i = 0; $i -lt $externalServicesNamesAndPorts.count; $i += 2) {
$serviceName = $externalServicesNamesAndPorts[$i]
$servicePort = $externalServicesNamesAndPorts[$i + 1]
$netStatCmd = "netstat -na | findstr /r ':"+$servicePort+".*LISTENING'"
if (Invoke-Expression $netStatCmd) {
Write-ColorOutput green ($serviceName + " is running on port:"+$servicePort)
} else {
Write-ColorOutput red ($serviceName + " is NOT running on port:"+$servicePort)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment