Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CPU usage & Memory consumed by particular process in powershell script

Tags:

powershell

I want to find performance of single process, as example "SqlServer"

Which commands I should write to find out 2 things:

  1. RAM utilized by SqlServer
  2. CPU utilized by SqlServer

I found lot of solutions listing all processes, but I want to get only 1 i.e. SqlServer.

like image 991
AK47 Avatar asked Jun 11 '14 06:06

AK47


2 Answers

The command to get SQL server process information:

Get-Process SQLSERVR

The command to get information for any process that starts with S:

Get-Process S*

To get the amount of virtual memory that the SQLServer process is using:

Get-Process SQLSERVR | Select-Object VM

To get the size of the working set of the process, in kilobytes:

Get-Process SQLSERVR | Select-Object WS 

To get the amount of pageable memory that the process is using, in kilobytes:

Get-Process SQLSERVR - Select-Object PM

To get the amount of non-pageable memory that the process is using, in kilobytes:

Get-Process SQLSERVR - Select-Object NPM

To get CPU (The amount of processor time that the process has used on all processors, in seconds):

Get-process SQLSERVR | Select-Object CPU

To better understand the Get-Process cmdlet, check out the documentation on technet here.

like image 86
D3vtr0n Avatar answered Nov 16 '22 04:11

D3vtr0n


About the CPU I got this working the following way:

# To get the PID of the process (this will give you the first occurrance if multiple matches)
$proc_pid = (get-process "slack").Id[0]

# To match the CPU usage to for example Process Explorer you need to divide by the number of cores
$cpu_cores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors

# This is to find the exact counter path, as you might have multiple processes with the same name
$proc_path = ((Get-Counter "\Process(*)\ID Process").CounterSamples | ? {$_.RawValue -eq $proc_pid}).Path

# We now get the CPU percentage
$prod_percentage_cpu = [Math]::Round(((Get-Counter ($proc_path -replace "\\id process$","\% Processor Time")).CounterSamples.CookedValue) / $cpu_cores)
like image 24
Angel Abad Cerdeira Avatar answered Nov 16 '22 03:11

Angel Abad Cerdeira