locked
"telnet" connection test to different servers on different ports RRS feed

  • Question

  • Im attempting to script a way to test connections to different servers on different ports.  In this example I want to test Port 80 on 10.1.2.3 -- port 443 on 10.1.2.4 and port 8080 on 10.1.2.5. 

    I can get it to work fine with a single port listed, it will test that port on all servers in the list but cant get it to work to test the listed port on the listed server. 

    $servers = '10.1.2.3','10.1.2.4','10.1.2.5'
    $ports = '80','443','8080'
    
    foreach ($server in $servers) {
    $source= $server
    $test=$port
      
    
        If ( Test-Connection $server -Count 1) {
     
            try {       
                $null = New-Object System.Net.Sockets.TCPClient -ArgumentList ($source,$test)
                $props = @{
                    Server = $source
                    PortChecked = $test
                    PortOpen = 'Yes'
                }
            }
    
            catch {
                $props = @{
                    Server = $source
    		PortChecked = $test
                    PortOpen = 'No'
                }
            }
        }
    
        Else {
            
            $props = @{
                Server = $server
                PortOpen = 'Server did not respond to ping'
            }
        }
    
        New-Object PsObject -Property $props
    
    }
    

    Monday, February 5, 2018 4:36 PM

Answers

  • Please review the following carefully:

    $servers = @{
      '10.1.2.3' = 80
      '10.1.2.4' = 443
      '10.1.2.5' = 8080
    }
    $ProgressPreference = 'SilentlyContinue'
    $servers.Keys |
        ForEach-Object{
            Test-NetConnection -ComputerName $_ -Port $servers[$_]
        }


    \_(ツ)_/

    • Proposed as answer by clayman2 Tuesday, February 6, 2018 12:30 PM
    • Marked as answer by 4114n__ Wednesday, February 7, 2018 3:25 PM
    Tuesday, February 6, 2018 3:48 AM

All replies

  • Try using "Test-NetConnection" to check the ports.

    After your foreach loop for the IP Address, I would create another foreach loop to go through each of the ports for each of the IPs.

    Let us know how you get on.

    Monday, February 5, 2018 4:44 PM
  • If you only want to test a specific port to a specific server, rather than using two arrays, use a hashtable

    $serverPort = @{
      '10.1.2.3' = '80'
      '10.1.2.4' = '443'
      '10.1.2.5' = '8080'
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, February 5, 2018 5:03 PM
  • Try below

    $Servers = @{
       10.1.2.4 = 80
       10.1.2.5 = 443
       10.1.2.6 = 53
    }
    
    $Servers.GetEnumerator().Name | Foreach-Object -Process{
       $Destination = $_
       $Port        = $Servers[$_]
       $null = New-Object System.Net.Sockets.TCPClient -ArgumentList ($Destination,$Port)
    
    }


    Regards kvprasoon


    Monday, February 5, 2018 6:06 PM
  • Well ...

    $servers = '10.1.2.3','10.1.2.4','10.1.2.5'
    $ports = 80,443,8080
    
    foreach ($server in $servers) {
        foreach ($port in $ports) {
            Test-NetConnection -ComputerName $server -Port $port
        }
    }


    \_(ツ)_/


    • Edited by jrv Monday, February 5, 2018 7:31 PM
    Monday, February 5, 2018 7:30 PM
  • Well ...

    $servers = '10.1.2.3','10.1.2.4','10.1.2.5'
    $ports = 80,443,8080
    
    foreach ($server in $servers) {
        foreach ($port in $ports) {
            Test-NetConnection -ComputerName $server -Port $port
        }
    }


    \_(ツ)_/


    Yup that would be it, but he only wants to test a certain port against a certain server. The above will test ALL ports on ALL servers. :-)

    Which is why I recommended using a Hash Table


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Monday, February 5, 2018 7:46 PM typo
    Monday, February 5, 2018 7:43 PM
  • Thank you kvprasoon.  I tested your example and it semi works but I get the below error.  It does appear to take the correct server and port however errors out. 

    A null key is not allowed in a hash literal.

    At D:\Changes\TelnetTest2.ps1:2 char:1

    + 10.32.35.84 = 80

    + ~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException

        + FullyQualifiedErrorId : InvalidNullKey

    New-Object : Exception calling ".ctor" with "2" argument(s): "No connection could be made because the target machine actively refused it

    10.32.35.83:443"

    At D:\Changes\TelnetTest2.ps1:10 char:12

    +    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList ($Destination,$ ...

    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException

        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    New-Object : Exception calling ".ctor" with "2" argument(s): "No connection could be made because the target machine actively refused it

    10.32.35.84:80"

    At D:\Changes\TelnetTest2.ps1:10 char:12

    +    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList ($Destination,$ ...

    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException

        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    New-Object : Exception calling ".ctor" with "2" argument(s): "No connection could be made because the target machine actively refused it

    10.32.35.82:8080"

    At D:\Changes\TelnetTest2.ps1:10 char:12

    +    $null = New-Object System.Net.Sockets.TCPClient -ArgumentList ($Destination,$ ...

    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException

        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Monday, February 5, 2018 7:48 PM
  • The hash table entries need to be quoted

    $Servers = @{
       '10.1.2.4' = '80'
       '10.1.2.5' = '443'
       '10.1.2.6' = '53'
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Monday, February 5, 2018 7:53 PM
  • Can you show us the Hashtable you created ?

    User PowerShell ISE or VSCode and debug it, it will help you a  lot.


    Regards kvprasoon


    Tuesday, February 6, 2018 3:41 AM
  • Please review the following carefully:

    $servers = @{
      '10.1.2.3' = 80
      '10.1.2.4' = 443
      '10.1.2.5' = 8080
    }
    $ProgressPreference = 'SilentlyContinue'
    $servers.Keys |
        ForEach-Object{
            Test-NetConnection -ComputerName $_ -Port $servers[$_]
        }


    \_(ツ)_/

    • Proposed as answer by clayman2 Tuesday, February 6, 2018 12:30 PM
    • Marked as answer by 4114n__ Wednesday, February 7, 2018 3:25 PM
    Tuesday, February 6, 2018 3:48 AM
  • Please review the following carefully:

    $servers = @{
      '10.1.2.3' = 80
      '10.1.2.4' = 443
      '10.1.2.5' = 8080
    }
    $ProgressPreference = 'SilentlyContinue'
    $servers.Keys |
        ForEach-Object{
            Test-NetConnection -ComputerName $_ -Port $servers[$_]
        }


    \_(ツ)_/


    This worked perfectly!  Thank you!!
    Wednesday, February 7, 2018 3:25 PM