Portscan against a single host

Hi all,

I would like to write a one-liner to scan a host for a list of predefined ports and then output that to a csv so I can import to excel and then manipulate the data. From the console the below works fine (without the export-csv), but when when I export it to excel the output is screwed. I also tried out-file and had similar results. you may recognise the ports, they are the ports required for vmware virtual centre. Thanks in advance.

$ports = “80”,”88”,”389”,”443”,”902”,”903”,”1234”,”1235”,”2012”,”2013”,”2014”,”8080”,”8085”,”8089”,”8443”,”60099”,”6501”,”6502”,”7005”,”7009”,”7080”,”7331”,”7343”,”7444”,”8000”,”8009”,”8100”,”8182”,”8200”,”9000”,”9001”,”9002”,”9003”,”9004”,”9005”,”9006”,”9007”,”9008”,”9009”,”9010”,”9443”,”9875”,”9876”,”9877”,”9090”,”10080”,”10111”,”10443”,”11711”,”11712”,”12721”,”8190”,”8191”,”22000”,”22100”,”31010”,”31100”,”32010”,”32100”,”12443”

foreach($port in $ports){ Test-NetConnection -ComputerName 'the computer I was to port scan' -port $port | export-csv -notypeinformation 'pathtothefiletoexprot' }

Works ok for me. Pipe it to select first if you want fewer properties. There’s more properties than get printed by default. Welcome to Powershell. I wish test-netconnection had a timeout option. You might like this invoke-tspingsweep script. Ping and port scan with a little .net added to powershell is pretty easy. Use PowerShell for Network Host and Port Discovery Sweeps - Scripting Blog

Thanks but I disagree. Yes the script works, but the export of the results doesn’t and that’s what I wan’t, the resultsto a CSV.

move export-csv outside of a loop

$result = foreach ...
$resilt | export-csv ...

or change foreach to foreach-object

$ports | foreach-obeject { Test-NetConnection -Port $_ -Comp... } | Export-csv ...

My bad. I should have noticed the csv only had 2 rows. I wish the -port option took a list too.

You mean like:

 $Ports = Get-Content .\portlist.txt # portlist.txt being the list of your ports

I mean like:

Test-NetConnection -ComputerName 'the computer I was to port scan' -port $ports

Well you still need to loop the list of ports somehow, best you could do would be something like this:

foreach($port in (Get-Content .\portlist.txt)){ Test-NetConnection -ComputerName 'the computer I was to port scan' -port $port}

Thanks Max that’s what I needed (and forgot to do), now I need a drink!

:slight_smile:
btx, get rid of quotes around ports, it not strings, it is integers

$ports = 80,443, ...