How can I automate Telnet port checking in Powershell?` How can I automate Telnet port checking in Powershell?` powershell powershell

How can I automate Telnet port checking in Powershell?`


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    }}


Here is a complete powershell script that will:

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

checklist.csv

remoteHost,portlocalhost,80asdfadsf,83localhost,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"