PoshCode Archive  Artifact [580b94473f]

Artifact 580b94473f39d733c4181b604f40026df4e953d2f50932f2b97f9bde4cda87de:

  • File Get-ProcessCount.ps1 — part of check-in [7dac5ea715] at 2018-06-10 14:00:12 on branch trunk — Get-ProcessCount returns the number of running processes on local or remote machine. If it can’t find the requested process, it tries to guess what you want. (user: Bas Bossink size: 1318)

# encoding: ascii
# api: powershell
# title: Get-ProcessCount
# description: Get-ProcessCount returns the number of running processes on local or remote machine. If it can’t find the requested process, it tries to guess what you want.
# version: 0.1
# type: function
# author: Bas Bossink
# license: CC0
# function: Get-ProcessCount
# x-poshcode-id: 573
# x-archived: 2008-09-15T21:34:00
#
#
# Get-ProcessCount uses 2 main variables, server and process name.
# Process name is typically the end exe, such as "svchost.exe"
# Will accept unnamed args (Get-ProcessCount servername processname)
# or named args (Get-ProcessCount -Computer servername -Process processname)
Function Get-ProcessCount([string]$process, [string]$computer = "localhost", [switch]$guess) {
	if($guess) {
		$clause = [string]::Format("like '%{0}%'",$process)
	}
	else {
		$clause = [string]::Format("='{0}'",$process)
	}
	#using string.Format can be very nice to do variable substitution
	$selectstring = [string]::Format("select * from Win32_Process where name {0}",
		$clause)
	$result = get-wmiobject -query $selectstring -ComputerName $computer
	# I really like the group-object cmdlet for reporting stuff 
	if($result) { $result | Group-Object Name }
	else { Write "Process $process could not be found" }
}