3

I'm currently trying to put together a script that queries AD for a list of computers, pings the computers to determine which ones are still active, and then telnets into a specific port on all the pingable computers. The output I'm looking for is a full list of pingable computers in AD for which I can't telnet to the said port.

I've read these few questions, but they don't quite hit on what I'm trying to do. I just want to see if the telnet connection is successful without entering telnet (or automate the quitting of telnet) and move on to the next machine to test. The AD and pinging portions of my script are set, I'm just stuck here. The things I've tried haven't quite worked as planned.

Here is the code for the first parts of the script, if needed:

Get-ADComputer -Filter * -SearchBase 'DC=hahaha,DC=hehehe' | ForEach {

$computerName = $_.Name

$props = @{
    ComputerName = $computerName
    Alive = $false
    PortOpen = $false
}

If (Test-Connection -ComputerName $computerName -Count 1 -Quiet) {

    $props.Alive = $true
}
Community
  • 1
  • 1
Justin
  • 35
  • 1
  • 1
  • 6
  • 1
    How about the simple code example from [here](http://www.powershelladmin.com/wiki/Check_for_open_TCP_ports_using_PowerShell). You have to attempt some connection to test if the port is open. – Matt Oct 08 '14 at 16:24
  • Thanks for the suggestion, Matt! I added that code (tweaked to fit what I have) but it returned every computer as having the port closed, which I know not to be true. I'm a relative newbie with both telnet and powershell, but I'm convinced here that I need to be using telnet to actually make the determination I need. – Justin Oct 08 '14 at 17:34
  • The `telnet` command does the exact same thing as the code @Matt suggested. – Ansgar Wiechers Oct 08 '14 at 18:39
  • To verify whether the port is actually opened on the target box, run `netstat -an | findstr LISTENING | findstr ":80"` on it. Here 80 is the port you are looking for so change it to desired port. If the command lists your port then the issue will be with a firewall, most likely Windows Firewall on the target box. – Raf Oct 09 '14 at 08:51
  • Good ol' user error. I got the code that @Matt suggested to work after I figured out what I was doing wrong (variable definitions). Thanks for all your help, guys! – Justin Oct 09 '14 at 17:43

2 Answers2

7

Adapting this code into your own would be the easiest way. This code sample comes from the PowerShellAdmin wiki. Collect the computer and port you want to check. Then attempt to make a connection to that computer on each port using Net.Sockets.TcpClient.

foreach ($Computer in $ComputerName) {

    foreach ($Port in $Ports) {

        # Create a Net.Sockets.TcpClient object to use for
        # checking for open TCP ports.
        $Socket = New-Object Net.Sockets.TcpClient

        # Suppress error messages
        $ErrorActionPreference = 'SilentlyContinue'

        # Try to connect
        $Socket.Connect($Computer, $Port)

        # Make error messages visible again
        $ErrorActionPreference = 'Continue'

        # Determine if we are connected.
        if ($Socket.Connected) {
            "${Computer}: Port $Port is open"
            $Socket.Close()
        }
        else {
            "${Computer}: Port $Port is closed or filtered"  
        }
        # Apparently resetting the variable between iterations is necessary.
        $Socket = $null
    }
}
Matt
  • 45,022
  • 8
  • 78
  • 119
5

Here is a complete powershell script that will:

1. read the host and port details from CSV file
2. perform telnet test
3. write the output with the test status to another CSV file

checklist.csv

remoteHost,port
localhost,80
asdfadsf,83
localhost,135

telnet_test.ps1

$checklist = import-csv checklist.csv
$OutArray = @()
Import-Csv checklist.csv |`
ForEach-Object { 
    try {
        $rh = $_.remoteHost
        $p = $_.port
        $socket = new-object System.Net.Sockets.TcpClient($rh, $p)
    } catch [Exception] {
        $myobj = "" | Select "remoteHost", "port", "status"
        $myobj.remoteHost = $rh
        $myobj.port = $p
        $myobj.status = "failed"
        Write-Host $myobj
        $outarray += $myobj
        $myobj = $null
        return
    }
    $myobj = "" | Select "remoteHost", "port", "status"
    $myobj.remoteHost = $rh
    $myobj.port = $p
    $myobj.status = "success"
    Write-Host $myobj
    $outarray += $myobj
    $myobj = $null
    return
}
$outarray | export-csv -path "result.csv" -NoTypeInformation

result.csv

"remoteHost","port","status"
"localhost","80","failed"
"asdfadsf","83","failed"
"localhost","135","success"
Ahamed Fasil
  • 51
  • 1
  • 1