【问题标题】:Powershell Get CPU PercentagePowershell 获取 CPU 百分比
【发布时间】:2012-06-22 23:45:42
【问题描述】:

对于如何在 Powershell 中获取进程的 CPU 百分比似乎没有任何简单的解释。我已经用谷歌搜索并在这里搜索过,但我没有看到任何确定的东西。有人可以用外行术语解释如何获得进程的 CPU 百分比吗?谢谢!

这里有一些东西可以帮助你开始;)

$id4u = gps | ? {$_.id -eq 412}
function get_cpu_percentage {
# Do something cool here
}
get_cpu_percentage $id4u

【问题讨论】:

    标签: performance powershell cpu


    【解决方案1】:

    使用 WMI:

    get-wmiobject Win32_PerfFormattedData_PerfProc_Process | ? { $_.name -eq 'powershell' } | select name,  PercentProcessorTime
    

    功能:

        function get_cpu_percentage ($id )
    {
    (get-wmiobject Win32_PerfFormattedData_PerfProc_Process | ? { $_.idprocess -eq $id.id }).PercentProcessorTime
    }
    
    $id4u = gps | ? {$_.id -eq 412}
    get_cpu_percentage -id $id4u
    

    【讨论】:

      【解决方案2】:

      这个怎么样?

      gps powershell_ise | Select CPU
      

      请注意,这是一个 scriptProperty,不会为远程系统显示任何内容。这是众所周知的。

      【讨论】:

      • gps wmplayer |选择 cpu 给我的值为 683.484375。我正在寻找 100% 的百分比。我不确定如何根据周期计算总 CPU 百分比。
      【解决方案3】:

      get-counter 提供有关系统性能的信息。您也可以获取有关单个进程的信息。这里有更多关于它的信息:http://social.technet.microsoft.com/Forums/lv/winserverpowershell/thread/8d7502d4-9e67-43f7-94da-01755b719cf8 和这里http://blogs.technet.com/b/heyscriptingguy/archive/2010/02/16/hey-scripting-guy-february-16-2010a.aspx 我不确定这是否是您要查找的内容,但这是使用进程名称和获取计数器的一种可能性。

      注意:如果您有超过 1 个处理器,我认为您应该将结果除以处理器数量。您可以使用以下方法获取此值:

      $env:NUMBER_OF_PROCESSORS
      
      
      
          function get_cpu_percentage {
          param([string[]]$myProc)
          return (get-counter -Counter "\Process($myProc)\% processor time" -SampleInterval 5 -MaxSamples 5 | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
          }
          get_cpu_percentage ("powershell")
      

      或使用 proc id 和 gps:

      注意:我相信 get-process 返回的值是该进程自启动以来使用的 CPU 秒数。这不是使用的 cpu 百分比。

          function get_cpu_since_start{
          param ([system.object]$p)
          $id4ucpu = $p | select-object CPU
          return $id4ucpu.CPU
          }
      get_cpu_since_start (gps | ? {$_.id -eq 412})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        • 1970-01-01
        • 2020-11-27
        • 2011-10-10
        • 2012-10-19
        • 1970-01-01
        相关资源
        最近更新 更多