Quantcast
Viewing all articles
Browse latest Browse all 20518

Counting Instances of a Process - Help!

What started off as a straight-forward exercise in counting the number of processes running on a given server has made me spend way too many hours chasing my tail, so I am reaching out for some community help.

 

I start off with the great template from xander.snyder but had to do a little tweaking.  While I have a poller on the same network as the target computer I need to use different credentials to query the server.  Here is what I have so far -- note that I've tried to use both the Get-Process and Get-WMIobject cmdlets.  I've even tried the $process = Invoke-Command -ComputerName SERVER1.domain.com -ScriptBlock {Get-Process -Name $args[0]} as well.  Note that $args[0] is the process name and $args[1] and $args[2] are the warn and critical threshold counts.

 

Every time I run a test from within SAM I get the same count -- 0.  I've connected to my remote poller (and yes, my test node is on that remote poller, not another poller) to I am able to query the test server using PowerShell and WMI (wbemtest or WMI Explorer) and they return the correct values and/or counts.  I am currently using a Windows Powershell Monitor with the code below and with the component set up as follows:

 

Image may be NSFW.
Clik here to view.
2014-08-05_13h36_02.png

 

And I still get the following when I test -- any ideas?

 

Image may be NSFW.
Clik here to view.
2014-08-05_13h35_33.png

 

$ErrorActionPreference = "silentlycontinue";

#$process = Get-Process -Name $args[0]; #Sets the variable $process to the the process name in the script arguments.

$process = get-wmiobject -query "SELECT * FROM Win32_Process WHERE name='$args[0]'" -computername SERVER1.domain.com;

$warn = $args[1]; #Sets the warning threshold to the the second argument in the script arguments.

$crit = $args[2]; #Sets the critical threshold to the the thrid argument in the script arguments.

$count = ($process | Measure-Object).Count;

 

switch ($count)

  {

  {$count -eq 0} {Write-Host 'Statistic:' $count; exit(1); break}

 

# If the process is not running the script exits as down.

 

  {$count -ge $crit} {Write-Host 'Statistic:' $count; exit(3); break}

 

# If the count is less than or equal to the Warning Threshold the script returns the count and exits as Warning.

 

  {$count -ge $warn} {Write-Host 'Statistic:' $count; exit(2); break}

 

# If the count is greater than or equal to the Critical Threshold the script returns the count and exits as Critical.

 

  default {Write-Host 'Statistic:' $count; exit(0)}

 

# If the process is running and does not match the passed Warning or Critical thresholds the script returns the count and exits as Up.

  }


Viewing all articles
Browse latest Browse all 20518

Trending Articles