2012-06-16 49 views
4

我想获取具有进程ID的进程的特定计数器。但是我想不出使用where-object来匹配计数器进程的方法。 像Powershell获取具有ID进程的特定进程计数器

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

因此使用进程ID检索名称和获得工作集(或大意的东西)。

回答

3

你可以得到一个进程的名字计数器,以便通过使用其ID,然后首先获得进程名将进程名称嵌入到计数器中。例如:

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

这对于进程的多个实例不起作用。 –

+0

你可以把它包装在一个foreach调用中,用你的proc ID管道获取每一个的信息。让我知道你是否需要一个例子。 –

+0

@KenJ我添加了一个适用于多个进程实例的答案。 – JPBlanc

1

使用get-process命令行程序很容易。只是筛选你想使用where-object进程ID输出,然后选择参数你有兴趣:

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

由于工作集 - 私有不存在,在Windows 2003中工作良好! –

5

这似乎有点令人费解,以获得正确的性能计数器路径具有相同进程名称的多个实例的过程:

$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 
2

如果你想一个解决方案,还包括工艺您可以使用多个实例ID:

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