我见过 - http://technet.microsoft.com/en-us/library/hh849832.aspx
而且,我看到了:How to get CPU usage & Memory consumed by particular process in powershell script
据我所知,我可以发送:get-process tomcat * | Powershell中的Select-Object CPU
我看到的最大问题是本地或远程都没有返回CPU ..
我已将以下脚本放在一起.. CPU部分正在运行..
writeHtmlHeader $TomcatMemFileName
writeTableHeader $TomcatMemFileName
[int]$i = 1
foreach ($server in Get-Content $serverlist)
{
$Computer=$server.split(",")[0]
$ip=$server.split(",")[1]
IF ( $Computer -notmatch 'DB' ) {
$procs = Gps tomcat* -ComputerName $Computer
foreach ($proc in $procs){
$id = $proc.ID
$machine = $proc.MachineName
$process = $proc.ProcessName
$WorkingSet = [int64]($proc.WorkingSet64/1024)
$VirtualMem = [int]($proc.VM/1MB)
$cpuse= Get-Counter -computername $machine '\Process(tomcat6)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property cookedvalue
foreach ($cpus in $cpuse){
$cpu=$cpus.cookedvalue
write-host $TomcatMemFileName $i $machine.ToUpper() $id $process $WorkingSet $VirtualMem $cpu
writeTomcatInfo $TomcatMemFileName $i $machine.ToUpper() $id $process $WorkingSet $VirtualMem $cpu
}
}
}
$i++
}
writeTablefooter $TomcatMemFileName
writeHtmlFooter $TomcatMemFileName
谢谢,
肯特
答案 0 :(得分:2)
您的CPU部分无法正常工作的原因是您将其传输到Format-Table(使用FT别名)。转到$cpuse =
行的末尾,然后移除| ft -AutoSize
。一旦你这样做,它应该按照你的预期工作。我还建议像Greg那样将过程过滤转移到柜台。
$cpuse= Get-Counter -computername $machine '\Process(tomcat*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property cookedvalue
答案 1 :(得分:1)
如果脚本的其余部分有效,我将假设您有权阅读计数器信息......
为什么不在计数器中指定进程名称,以便以后不必混淆所有过滤。例如:
$counter = Get-Counter '\Process(tomcat*)\% Processor Time' -ComputerName $myServer
$counter.CounterSamples.CookedValue
如果有多个tomcat进程,你必须拆开CounterSamples集合,但它应该可以正常工作。