【问题标题】:How to get CPU usage & Memory consumed by particular process in powershell script如何在powershell脚本中获取特定进程消耗的CPU使用率和内存
【发布时间】:2017-11-15 04:11:52
【问题描述】:

我想查找单个进程的性能,例如“SqlServer”

我应该写哪些命令来找出两件事:

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

我找到了很多列出所有进程的解决方案,但我只想获得 1 个,即 SqlServer。

【问题讨论】:

标签: powershell


【解决方案1】:

获取SQL server进程信息的命令:

Get-Process SQLSERVR

获取任何以 S 开头的进程的信息的命令:

Get-Process S*

获取 SQLServer 进程正在使用的虚拟内存量:

Get-Process SQLSERVR | Select-Object VM

获取进程工作集的大小,以千字节为单位:

Get-Process SQLSERVR | Select-Object WS 

要获取进程正在使用的可分页内存量,以千字节为单位:

Get-Process SQLSERVR - Select-Object PM

要获取进程正在使用的不可分页内存量,以千字节为单位:

Get-Process SQLSERVR - Select-Object NPM

获取CPU(进程在所有处理器上使用的处理器时间,以秒为单位):

Get-process SQLSERVR | Select-Object CPU

要更好地理解 Get-Process cmdlet,请查看文档 on technet here.

【讨论】:

  • 谢谢伙计!但为什么 VM 显示为减号 (-)。 VM 是否意味着 RAM?
  • 我浏览了那个链接,但无法理解 VM 是如何显示负值的?
  • 我不确定,因为我没有得到负值。你用的是什么命令?
  • 好的,现在可以得到正确的值了。在 VM 之外,WS .... 哪些值属于 RAM?在任务管理器中显示为内存(私有工作集)。
  • @AK47 刚刚看到这个帖子;值可能为负的原因是因为该值大于 INT 可以容纳的值。 (我认为是 4GB)
【解决方案2】:

关于 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)

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多