How to get CPU usage and memory consumed by a specific process in powershell script

I want to find the performance of a single process, for example, "SqlServer"

What commands should I write to learn 2 things:

  • RAM used by SqlServer
  • CPU used by SqlServer

I found many solutions listing all processes, but I want to get only 1 ieSqlServer.

+4
source share
2 answers

The command to get information about the SQL server process:

Get-Process SQLSERVR

The command to get information for any process starting with S:

Get-Process S*

To get the amount of virtual memory that the SQLServer process uses:

Get-Process SQLSERVR | Select-Object VM

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

Get-Process SQLSERVR | Select-Object WS 

, , :

Get-Process SQLSERVR - Select-Object PM

, , :

Get-Process SQLSERVR - Select-Object NPM

( , , ):

Get-process SQLSERVR | Select-Object CPU

Get-Process, .

+8

:

# 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)
+2

Source: https://habr.com/ru/post/1544094/


All Articles