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 <<<<