The other day I posted a snippet of code that I as using to monitor process memory utilization for a few web browsers. I thought the information and technique were useful enough to modularize in the form of a function. Perhaps I want to check working set on a different process or even on a different computer. This is what I came up with.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I created a function called Get-ProcessWS. That's awkard to type so you might consider adding an alias like gpws.
[cc lang="PowerShell"]
Function Get-ProcessWS {
[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a process name",
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string[]]$Name,
[ValidateNotNullOrEmpty()]
[string]$Computername=$env:Computername
)
Begin {
Write-Verbose "$(Get-Date) Starting $($myinvocation.command)"
} #begin
Process {
Foreach ($item in $Name) {
Write-Verbose "$(Get-Date) Checking $Item on $Computername"
#see if the process is running
$process=Get-Process -Name $Item -ComputerName $Computername -ea "silentlycontinue"
#only process if we got a process back
if ($process)
{
#get the earliest start time
$started= $process | Sort-Object -property StartTime | Select-Object -first 1
$measure=$process | Measure-Object -Property workingset -sum
#divide by 1MB
$ws=$measure.sum/1mb
#count the number of processes
$count=($process | Measure-Object).Count
#create a custom object
New-Object -TypeName PSObject -Property @{
Name=$Item
WorkingSet=$ws
Report=Get-Date
Started=$started.StartTime
Elapsed=(Get-Date)-($Started.StartTime)
Computername=$Computername
Count=$Count
}
} #if
else
{
Write-Verbose "$(Get-Date) Process not found"
}
} #foreach
} #Process
End {
Write-Verbose "$(Get-Date) Ending $($myinvocation.command)"
} #end
} #end function
[/cc]
The function takes one or more process names and attempts to retrieve those processes on the specified computer. The default is the localhost.
[cc lang="PowerShell"]
$process=Get-Process -Name $Item -ComputerName $Computername -ea "silentlycontinue"
[/cc]
I'm suppressing any exceptions that will occur if the process is not running. Assuming the process is found, the function retrieves and calculates some values.
[cc lang="PowerShell"]
if ($process)
{
#get the earliest start time
$started= $process | Sort-Object -property StartTime | Select-Object -first 1
$measure=$process | Measure-Object -Property workingset -sum
#divide by 1MB
$ws=$measure.sum/1mb
#count the number of processes
$count=($process | Measure-Object).Count
[/cc]
The function then writes a custom object to the pipeline. I've added a few items from my original code such as the computer name and the number of processes.
[cc lang="PowerShell"]
#create a custom object
New-Object -TypeName PSObject -Property @{
Name=$Item
WorkingSet=$ws
Report=Get-Date
Started=$started.StartTime
Elapsed=(Get-Date)-($Started.StartTime)
Computername=$Computername
Count=$Count
}
[/cc]
The end result is an object like this:
[cc lang="DOS"]
Name : chrome
WorkingSet : 284.33203125
Elapsed : 14:18:14.5739805
Report : 5/12/2011 8:29:27 AM
Computername : SERENITY
Count : 9
Started : 5/11/2011 6:11:12 PM
[/cc]
You can pass process names as an array or pipe names to the function. The function writes to the pipeline so you can sort,convert, export, save to a file or whatever.
Download Get-ProcessWS.
Type
Get-ProcessWS ie*
or any other two char and a * that matches at least the first two chars of process name.
Try sv* as it will always exist. It should return either nothing or a list of matching proesses but it return only the first one found.
Why?
If Get-Process ie* returns something, then my function will process it. Which makes me realize I should tweak the function to return the name from the process object. Regardless, I’m not sure I follow your question. If I run my function for sv* I get the same 14 processes that I get running Get-Process sv*