Having a massive brain fart here… I’m attempting to build a script that my customers can use to run a quick health check on a program of ours. The program requires the server that our program is on to be able to communicate with other servers over specific ports. One of the things I’d like to do with this is to check all the connections from our server to the other servers and display if the connection was successful or not. I found this script, which so far works like a charm:
$hostname = 'HOSTNAME' $port = '104' function Test-Port($hostname, $port) { # This works no matter in which form we get $host - hostname or ip address try { $ip = [System.Net.Dns]::GetHostAddresses($hostname) | select-object IPAddressToString -expandproperty IPAddressToString if($ip.GetType().Name -eq "Object[]") { #If we have several ip's for that address, let's take first one $ip = $ip[0] } } catch { Write-Host "Possibly $hostname is wrong hostname or IP" return } $t = New-Object Net.Sockets.TcpClient # We use Try\Catch to remove exception info from console if we can't connect try { $t.Connect($ip,$port) } catch {} if($t.Connected) { $t.Close() $msg = "Port $port is operational" } else { $msg = "Port $port on $ip is closed, " $msg += "You may need to contact your IT team to open it. " } Write-Host $msg } Test-Port $hostname $port
The only thing I’m stuck on is trying to get the response from one remote machine to another. As it stands, I’m getting the results from the local workstation to the final destination. This doesn’t tell me if the connection is good between the 2 servers.