Author Posts

January 26, 2018 at 5:53 pm

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.

January 26, 2018 at 10:36 pm

You do not say which version of PoSH you are running, but...here is a different approach using all PoSH vs calling .NET
This is Q&D, but see that this does for your efforts and of course tweak in to your code...

    # Validate connection state of hosts in the environment.

    $AdComputers = (Get-ADComputer -Filter *).Name 

    ForEach($TargetHost in $AdComputers)
    {
        # Test from localhost to remote AD hosts
        Test-Connection -ComputerName $TargetHost -Count 1

        # Use current host valiadate remote hosts
        Invoke-Command -ComputerName $TargetHost -ScriptBlock {
            ForEach ($TargetHost in $Using:AdComputers)        
            {Test-Connection -ComputerName $TargetHost -Count 1}
        }
    }

    
    Source    Destination IPV4Address  IPV6Address  Bytes    Time(ms) 
    ------    ----------- -----------  -----------  -----    -------- 
    DC01      EX01        192.168...                32       0
    ...
    EX01      DC01        192.168...                32       0        
    ...       
        
   
    # If you want the port inclusion, then something like this.

    $AdComputers = (Get-ADComputer -Filter *).Name 

    ForEach($TargetHost in $AdComputers)
    {
        # Test from localhost to remote AD hosts
        "Local processing from source $env:COMPUTERNAME"
        Test-NetConnection -ComputerName $TargetHost -Port 445 | Format-Table -AutoSize

        # Use current host valiadate remote hosts
        Invoke-Command -ComputerName $TargetHost -ScriptBlock {
            ForEach ($TargetHost in $Using:AdComputers)        
            {
                "Remote processing from source $env:COMPUTERNAME"
                Test-NetConnection -ComputerName $TargetHost -Port 445 | Format-Table -AutoSize        
            }
        }
    }