简体   繁体   English

Powershell 获取具有 id process 的特定进程计数器

[英]Powershell Get a specific process counter with id process

I want to get specific counters for processes that I have process id's for.我想获取我拥有进程 ID 的进程的特定计数器。 However I can't think of a way to use where-object to match the process for the counter.但是我想不出一种方法来使用 where-object 来匹配计数器的过程。 Like

Where Gc '\process(*)\id process -eq 456 gc '\process($name)\working set'

So use the process id to retrieve the name and get the working set (or something to that effect).因此,使用进程 ID 检索名称并获取工作集(或类似的东西)。

It seems a bit convoluted to get the correct performance counter path for a process with multiple instances of the same process name:为具有相同进程名称的多个实例的进程获取正确的性能计数器路径似乎有点令人费解:

$proc_id=6580
$proc_path=((Get-Counter "\Process(*)\ID Process").CounterSamples | ? {$_.RawValue -eq $proc_id}).Path
Get-Counter ($proc_path -replace "\\id process$","\% Processor Time")

Timestamp                 CounterSamples
---------                 --------------
11/20/2014 5:39:15 PM     \\myhost\process(conhost#2)\% processor time :
                          0

You can get counters for a process name so first get the process name by using its Id and then embed the process name in the counter.您可以获得进程名称的计数器,因此首先使用其 Id 获取进程名称,然后将进程名称嵌入计数器中。 For example:例如:

$id = # your process id
$proc = (Get-Process -Id $id).Name
Get-Counter -Counter "\Process($proc)\% Processor Time"

If you want a solution that also include process with multiple instance IDs you can use:如果您想要一个还包括具有多个实例 ID 的进程的解决方案,您可以使用:

$p = $((Get-Counter '\Process(*)\ID Process' -ErrorAction SilentlyContinue).CounterSamples | % {[regex]$a = "^.*\($([regex]::Escape($_.InstanceName))(.*)\).*$";[PSCustomObject]@{InstanceName=$_.InstanceName;PID=$_.CookedValue;InstanceId=$a.Matches($($_.Path)).groups[1].value}})
# In french, use '\processus(*)\id de processus' for the counter name
$id = # your process id
$p1 = $p | where {$_.PID -eq $id}
Get-Counter -Counter "\Process($($p1.InstanceName+$p1.InstanceId))\% Processor Time"
# In french, use "\Processus($($p1.InstanceName+$p1.InstanceId))\% temps processeur" for the counter name

Or if you avoid to use Get-Counter and wait the sample interval, try use WMI:或者,如果您避免使用 Get-Counter 并等待采样间隔,请尝试使用 WMI:

$id = YourProcessIdHere
(gwmi -class Win32_PerfRawData_PerfProc_Process -Namespace "root\CIMV2" | ? {$_.IdProcess -eq $id}).Name;

It is possible to obtain some performance information with the Get-Process commandlet directly and avoid the need to resolve an instance ID.可以直接使用 Get-Process commandlet 获取一些性能信息,而无需解析实例 ID。

For the case of the memory working set, just filter the output for the process id you want using where-object , then select the parameters you're interested in:对于内存工作集的情况,只需使用where-object过滤您想要的进程 ID 的输出,然后选择您感兴趣的参数:

get-process | where-object{ $_.id -eq 456 } | select name,workingset

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

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