Running netstat from within Powershell script

I am trying to run the netstat command in a Powershell script and getting conflicting results.

If I run the following netstat command at the Powershell window, it returns the information just fine:
PS C:\tmp> $c = netstat -ban | select-string "F5FltSrv"; $c.count

Open in new window

But if I run a similar operator in the script, it keeps returning '0' (zero), so it fails. Attached powershell script for reference.

LVL 29
Michael WorshamStaff Infrastructure ArchitectAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is the script being run from an elevated session?
Michael WorshamStaff Infrastructure ArchitectAuthor Commented:
Yes. Administrator.
Really that's the only thing I can see that would affect the output.

Sorry if you're already well-aware of this, but even logged on as an administrator, if you have UAC turned on you will need to launch PowerShell using the "Run as administrator" option.
Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

The only way to return 0 would be for there to be no match. There are 2 ways to achieve no match.


The process name does not match your string so the match count is 0.


The script is not executing with elevated permissions and therefore returns a match count of 0.
What I would recommend you do is at the top of your script you add a check for administrator rights which is a super simple check to execute and will be good for your debugging in this case.

# F5_VPN_Check.ps1
# This simple script is used to determine when the F5 VPN Client is active or 
# has dropped. If the latter, the script will trigger a sound and a warning 
# message for the user to act upon.
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

if ($IsAdmin -eq $false)
	"script IS NOT elevated | out-file c:\somelogpath.txt
	## Time to exit
} else {
	"script IS elevated | out-file c:\somelogpath.txt

$VPN = 0

# Check to see how many VPN Sessions are active
$VPN = netstat -ban | select-string "F5FltSvr"


# Check to see if there are at least 8 F5 VPN processes running
if($VPN.count -gt 8) {
	Write-Host "F5 VPN Session is enabled." -b Green
else {
	Write-Host "F5 VPN Session is down!" -b Red -f Yellow
#	$sound = new-Object System.Media.SoundPlayer;
#	$sound.SoundLocation="c:\tmp\spoon1.wav";
#	$sound.Play();

Open in new window

Michael WorshamStaff Infrastructure ArchitectAuthor Commented:
Actually found the problem after getting a 2nd pair of eyes on the script.

In the script test, I was using select-string "F5FltSvr".

When in fact I should have been using what was seen in the command-line: select-string "F5FltSrv".

Gotta love typos.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael WorshamStaff Infrastructure ArchitectAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Michael W's comment #a40876008

for the following reason:

Having an extra pair of eyes can fix many problems for those with lack of sleep.
So you're saying that my answer was correct when I said if the string does not match it will return 0? :)
Michael WorshamStaff Infrastructure ArchitectAuthor Commented:
Sorry, but "string not matching" is not an technical enough answer and the expert should have been better documented the 'exact' problem and where it lies. Someone from StackExchange actually found the error / typo and my answer pasted here provided the actual fix. Also, the script does not have to run at elevated session as the processes being queried are at the user level, not system.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.