I've written a script to get port information on a single port. The script pipes 'netstat -anob' into a variable then uses Where-Object to scan the object for the exact port number and spit back the desired info.
Unfortunately, the only conditional parameter I use that gets a positive result is -match. But it also yields false positives.
$netstatport = $netstat | Where-Object { $_ -match $port }
If the port I'm searching for is, for example, 555, then the info is found. But the same info is also found if $port is equal to 55. In other words, what I need is:
$port = 55 >> False
$port = 555 >> True
$port = 5555 >> False
I've tried various conditions like -eq, -ieq, =, and so on. Only -match works, but it's too imprecise.
What am I missing?
Where-Object {$_ -match "\b"+$port+"\b"}orWhere-Object {$_ -match "\b$port\b"}?