#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-ProcessWS.ps1 # Version: 0.9 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # http://www.scriptinggeek.com # Date: 5/12/2011 # Keywords: Process, WorkingSet # Comments: # # "Those who forget to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Get-ProcessWS { <# .SYNOPSIS Get process workingset and run time .DESCRIPTION This function will retrieve specified processes from a specifed computer and write an object to the pipeline that shows the processes workingset in MB, when it was started and how long it has been running. The workingset value is in MB. Name : chrome Started : 5/11/2011 6:11:12 PM Workingset : 284.0703125 Elapsed : 14:09:22.9069674 Report : 5/12/2011 8:20:35 AM Count : 9 Computername : SERENITY For multiple instances of the process name, the workingset will be the sume of all processes and the start time will be calculated from the earliest process. .PARAMETER Name The name of the process to check .PARAMETER Computername The name of a computer to check. The default is the local host. .EXAMPLE PS C:\> Get-ProcessWS "Chrome","Firefox","IExplore" 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 Name : firefox WorkingSet : 141.78125 Elapsed : 00:27:57.8125551 Report : 5/12/2011 8:29:27 AM Computername : SERENITY Count : 1 Started : 5/12/2011 8:01:29 AM Name : iexplore WorkingSet : 169.75390625 Elapsed : 00:03:31.0212963 Report : 5/12/2011 8:29:27 AM Computername : SERENITY Count : 3 Started : 5/12/2011 8:25:56 AM Compare working set usage for three browsers. .NOTES NAME : Get-ProcessWS VERSION : 0.9 LAST UPDATED: 5/12/2011 AUTHOR : SERENITY\Jeff .LINK http://jdhitsolutions.com/blog/2011/05/get-process-detail/ .LINK Get-Process Measure-Object .INPUTS String .OUTPUTS Custom object #> [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