This topic contains 9 replies, has 4 voices, and was last updated by Max Kozlov 10 months ago.
-
AuthorPosts
-
June 21, 2017 at 7:05 am #73346
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' }
-
June 21, 2017 at 2:52 pm #73370
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. https://blogs.technet.microsoft.com/heyscriptingguy/2012/07/02/use-powershell-for-network-host-and-port-discovery-sweeps/
-
June 22, 2017 at 7:21 am #73412
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.
-
-
June 22, 2017 at 10:25 am #73420
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 ...
-
June 22, 2017 at 2:18 pm #73442
My bad. I should have noticed the csv only had 2 rows. I wish the -port option took a list too.
-
June 22, 2017 at 3:47 pm #73484
You mean like:
$Ports = Get-Content .\portlist.txt # portlist.txt being the list of your ports
-
June 22, 2017 at 3:52 pm #73489
I mean like:
Test-NetConnection -ComputerName 'the computer I was to port scan' -port $ports
-
June 22, 2017 at 4:43 pm #73499
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}
-
June 23, 2017 at 5:11 am #73543
Thanks Max that's what I needed (and forgot to do), now I need a drink!
-
-
June 23, 2017 at 6:36 am #73546
🙂
btx, get rid of quotes around ports, it not strings, it is integers
$ports = 80,443, ...
-
AuthorPosts
You must be logged in to reply to this topic.