使用Powershell获取特定进程的CPU% [英] Get CPU % of a particular process using powershell

查看:92
本文介绍了使用Powershell获取特定进程的CPU%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用powershell命令获取特定进程的CPU使用率(而不是处理器时间).

I want to get the CPU usage % (not processor time) of a particular process using a powershell command.

示例:(Windows 8任务管理器)

Example: (Windows 8 Task Manager)

我想通过命令获得该 2.9%.

推荐答案

这是正确的答案,这是支持案例,那么您有多个具有相同名称的进程https://stackoverflow.com/a/34844682/483997

Here is the correct answer which is support case then you have multiple processs with same name https://stackoverflow.com/a/34844682/483997

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

这篇关于使用Powershell获取特定进程的CPU%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆