简体   繁体   English

如何在PowerShell脚本中获取特定进程所消耗的CPU使用率和内存

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

I want to find performance of single process, as example "SqlServer" 我想找到单个进程的性能,例如“SqlServer”

Which commands I should write to find out 2 things: 我应该写哪些命令来找出两件事:

  1. RAM utilized by SqlServer SqlServer使用的RAM
  2. CPU utilized by SqlServer SqlServer使用的CPU

I found lot of solutions listing all processes, but I want to get only 1 ie SqlServer. 我发现很多解决方案列出了所有进程,但我想只得到1即SqlServer。

The command to get SQL server process information: 获取SQL Server进程信息的命令:

Get-Process SQLSERVR

The command to get information for any process that starts with S: 获取以S开头的任何进程的信息的命令:

Get-Process S*

To get the amount of virtual memory that the SQLServer process is using: 要获取SQLServer进程正在使用的虚拟内存量:

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): 获取CPU(进程在所有处理器上使用的处理器时间量,以秒为单位):

Get-process SQLSERVR | Select-Object CPU

To better understand the Get-Process cmdlet, check out the documentation on technet here. 若要更好地了解Get-Process cmdlet,请在此处查看technet上的文档

About the CPU I got this working the following way: 关于CPU我通过以下方式工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM