Check number of TCP connections

Viewed 374

I need to check number of connections on given computer and if it is greater than 100 - kill process. My code:

IF ((Get-counter -Counter "\TCPv4\Connections Established" | Measure-Object).Length -gt 100)
{
  stop-process -name Notepad
  Write-host Process killed!
}
Else {
  Write-host OK!
}

It is not working. How to convert "Connections Established" to string and make it work? Newbie in PowerShell. Thanks.

I use script:

IF ((Get-Counter -Counter "\TCPv4\Connections Established").CounterSamples.CookedValue -gt 100)
{
stop-process -name Notepad
Write-host Process killed!
}
Else {
Write-host not ok.
}

output:

PS C:\Users\Administrator> D:\Share\scriptif.ps1
DEBUG:    1+  <<<< D:\Share\scriptif.ps1
DEBUG:     ! CALL function 'scriptif.ps1'  (defined in file 'D:\Share\scriptif.ps1')
DEBUG:    2+ IF <<<<  ((Get-counter -Counter "\TCPv4\Connections Established").CounterSamples.cookedvalue -gt 100)
DEBUG:    8+  <<<< Write-host not ok.
not ok.
DEBUG:    2+         $foundSuggestion = <<<<  $false
DEBUG:     ! SET $foundSuggestion = 'False'.
DEBUG:    4+         if <<<< ($lastError -and
DEBUG:   15+         $foundSuggestion <<<<

When I try just (in command line):

(Get-Counter -Counter "\TCPv4\Connections Established").CounterSamples.CookedValue

I have empty result.

I tried new way with debug with no effect:

PS C:\Users\Administrator> (Get-Counter -Counter "\TCPv4\Connections Established").CounterSamples.CookedValue
DEBUG:    1+ (Get-Counter -Counter "\TCPv4\Connections Established").CounterSamples. <<<< CookedValue
DEBUG:    2+         $foundSuggestion = <<<<  $false
DEBUG:     ! SET $foundSuggestion = 'False'.
DEBUG:    4+         if <<<< ($lastError -and
DEBUG:   15+         $foundSuggestion <<<<
1 Answers

Going step by step you can examine your output to see where you can find the number you're interested in. Let's start with checking what Get-Counter gives you:

PS> (Get-counter -Counter "\TCPv4\Connections Established")

Timestamp                 CounterSamples
---------                 --------------
2019-10-09 11:20:09       \\hostname\tcpv4\connections established :
                          190

You have to get data from CounterSamples property, so let's examine that one:

PS> (Get-counter -Counter "\TCPv4\Connections Established").countersamples

Path                                         InstanceName CookedValue
----                                         ------------ -----------
\\hostname\tcpv4\connections established                      159

As you can see, what you want to find is in CookedValue property so you can change your if statement to:

if ((Get-counter -Counter "\TCPv4\Connections Established").countersamples.cookedvalue -gt 100) {
  # do something
}

Edit: as per comments, this is desired output with debug enabled (comments starting with # were added by me):

# First I set debug
PS> Set-PSDebug -Trace 2

# Then I check CounterSamples
PS> (Get-counter -Counter "\TCPv4\Connections Established").countersamples
DEBUG:    1+  >>>> (Get-counter -Counter "\TCPv4\Connections Established").countersamples
DEBUG:     ! CALL function '<ScriptBlock>'

Path                                         InstanceName CookedValue
----                                         ------------ -----------
\\hostname\tcpv4\connections established                      186

# Then I check CookedValue
PS> (Get-counter -Counter "\TCPv4\Connections Established").countersamples.cookedvalue
DEBUG:    1+  >>>> (Get-counter -Counter "\TCPv4\Connections Established").countersamples.cookedvalue
DEBUG:     ! CALL function '<ScriptBlock>'
192

For PowerShell 2.0 compatibility use Select-Object instead of .CookedValue as below:

(Get-counter -Counter "\TCPv4\Connections Established").countersamples | Select-Object -ExpandProperty CookedValue
Related