Swampcritter
Swampcritter

Reputation: 44

Running netstat from within Powershell script

I am trying to monitor a F5 VPN Client running on my Windows 7 laptop when its established connection. I designed a simple Powershell script that would utilize the "netstat" command to look for a certain string variable that is only available when connection is established. I was planning on using Microsoft's Task Scheduler to trigger the script every so often to monitor the connection / string variable.

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
    9

But if I run a similar operator and variables in the Powershell script, it keeps returning '0' (zero), so it fails.

Here's the actual Powershell script I am using:

    $VPN = netstat -ban | select-string "F5FltSvr"

    Write-Host "DEBUG: " $VPN.count

    if($VPN.count -gt 7) {
    Write-Host "F5 VPN Session is enabled." -b Green
    exit
    }
    else {
    Write-Host "F5 VPN Session is down!" -b Red -f Yellow
    }

When executed

    PS C:\tmp> .\F5_VPN_Check.ps1
    0
    F5 VPN Session is down!

Can anyone tell me where I have a fault in the script? The Powershell window is running with Administrator priviledges and UAC is turned off.

Upvotes: 0

Views: 4786

Answers (1)

Keith Hill
Keith Hill

Reputation: 201662

Could it be a typo? Instead of this:

$VPN = netstat -ban | select-string "F5FltSvr"

You want this:

$VPN = netstat -ban | select-string "F5FltSrv"

At least that is the example you used that worked. Note the diff between Svr and Srv.

Upvotes: 1

Related Questions