Understanding PowerShell's $? and ncat Results

April 25, 2025 5 answers
Asked by !eddy7turn
I'm using PowerShell ISE 5.1.22621.4391 and trying to understand why I can't properly check the results of `ncat` commands for port scanning. When I run `ncat -zi5 STICATCDSDBPG1 5432`, the command returns `False` when checking `$?`, indicating failure, while it returns `True` for `FISSTAPPGS301`. Yet, when I try to evaluate the result like this: `if ((ncat -zi5 STICATCDSDBPG1 5432) -eq $true) { "open" } else { "closed" }`, it still shows "closed". I'm curious why it's not indicating whether the port is open or closed based on these commands. Any insights?

Answers

discrete choices
Also, check if your command outputs strings instead of booleans. If you're comparing to `True`, make sure it's not quoting `True` as a string if the output from `ncat` is a string instead! You can also try replacing `-eq` with `-match "True"` for string comparisons if that's applicable.

not quite convinced: Definitely worth checking! Filtering based on expected string output can help avoid confusion.

pita pocket: Good point! PowerShell can be tricky with type coercion.

Reply to this answer
_fifth_column_
You seem to be misunderstanding how PowerShell handles types. Since `ncat` produces no output on success, you're only seeing the boolean result indicating execution success. Thus, your comparison like `-eq $true` won't work as expected. It might be better to just compare the output directly to a string, or check `$LASTEXITCODE` like others have pointed out. For example, you could do: `if ($process.LastExitCode -eq 0) { 'open' } else { 'closed' }` after starting the process with `Start-Process`. It gives you a lot more control.

VACUUM GAUGE: For sure! It's vital to remember that `$LASTEXITCODE` will give you the return code that you can actually compare.

Snickety: Quite right! The way PowerShell handles comparisons can trip up even seasoned scripters.

Reply to this answer
dewpt-2C
The statement `(True -eq $true)` you're using is comparing a string to a boolean. That's why it's returning false. You can convert values to appropriate types before comparison or directly modify your call to check for equality to strings instead, like `"True" -eq (...)`. Switching to `Test-NetConnection` would also streamline your code significantly!

vintage_82mm: Great clarification! Correcting the data type matches can solve a lot of issues.

spot+12deg: Exactly! Aligning your data types is key in programming.

Reply to this answer
Brisket
The issue lies in the way `$?` works. It doesn't actually return the output of the command; instead, it indicates whether the command executed successfully or not. `ncat` gives a non-zero exit code when the port is closed, which makes `$?` return `False`. Instead of using `ncat` directly in your `if` condition, you might want to check the exit code directly with `$LASTEXITCODE`. Using `Test-NetConnection` could also be a simpler approach for checking the port status without involving `ncat`.

just browsing thanks: Absolutely! Using `Test-NetConnection` is definitely the PowerShell way. It's cleaner and designed for that purpose.

tHree short beeps: Yeah, and remember that `$?` reflects the success of the command you ran, not the data it returns.

Reply to this answer
POND SNAIL
Just a reminder, when comparing booleans or nulls in PowerShell, it's often safer to keep those on the left side of your comparisons. For example: `$true -eq $variable` tends to yield more reliable results. This makes sure that types are handled properly, avoiding unexpected conversion issues.

+check.the.manual: Couldnโ€™t agree more! Keeping track of types during these comparisons makes a big difference.

+beaver lodge~: Yes! Putting the boolean on the left helps avoid ambiguity.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions