2012-06-16 72 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"