1
votes

I'm trying to use a simple Port-Scanner as a PowerShell one-liner that looks like this:

1..1024 | % {echo ((new-object Net.Sockets.TcpClient).Connect("192.168.171.1",$_)) "Port $_ is open!"} 2>$null

It works perfectly in a PowerShell Window. But, if I try to start it from a Windows Command Window (CMD), I get the following error message:

enter image description here

2
In cmd, inner double quotes need to be escaped. Use \" - AdminOfThings

2 Answers

1
votes

Your quotes are conflicting. Try using:

powershell '1..1024 | % {echo ((new-object Net.Sockets.TcpClient).Connect("192.168.171.1",$_)) "Port $_ is open!"} 2>$null'
0
votes

Unless you on PowerShellv2, why not simplify this. Use the built-in cmdlets.

powershell -Command "1..1024 | ForEach {Test-NetConnection 192.168.171.1 -Port $PSitem}"

Absolutely, PowerShell is cleaner, the present/future, and where you want to be, but you could do this VBScript as well.

VBS Script to Check Port Availability in Windows