Powershell, problem with the comma character.

I'm using powershell a lot, and I want to use it like a normal shell environment, but I can't.

Well, I give you example, try this in powershell:

nmap -sS -p80,22 -PN -n 192.168.1.1

The comma character is the reason. It is a some kind of special character in powershell so you can't use it like that. In that case lets try the same command with ps escape character (backward apostrophe (`))

nmap -sS -p80`,22 -PN -n 192.168.1.1

still not working, but the error is different.

So how to fix this?

Solution number one (add space between -p and 80):

nmap -sS -p 80`,22 -PN -n 192.168.1.1

or solution number two (specify a protocol):

nmap -sS -pT:80`,22 -PN -n 192.168.1.1

2 comments:

LeeHolmes said...

Apologies - this is a bad example of two worlds colliding. In PowerShell, the comma represents an array. It thinks you are proving an array to the command, which it then converts to a string because native executables don't support PowerShell arrays. When it converts it to a string, it adds a space:

80,22 -> "80 22"

To get around this, you can also put the whole thing in quotes - I find that easier than escaping specific characters:

nmap -sS "-p80,22" ...

Here's more on the topic: http://powershell.labs.oreilly.com/ch01.html#resolve-errors-native-executables

Lee Holmes [MSFT]
Windows PowerShell Development

Kris Cieslak said...

Thanks for advice. It seems that we have few methods to "fix" that problem.

This also works:
nmap -sS -p "80,22"

(space between -p and "80,22" is important)

Post a Comment