如何使用PowerShell查找CPU和RAM使用情况?

Aar*_*ron 51 powershell

我试图让PowerShell给我RAM和CPU使用率,但我无法弄清楚要使用的WMI类.我的计算机有两个处理器,因此获取这两个处理器的信息会很有用.

Sha*_*evy 54

您还可以使用Get-Counter cmdlet(PowerShell 2.0):

Get-Counter '\Memory\Available MBytes'
Get-Counter '\Processor(_Total)\% Processor Time'
Run Code Online (Sandbox Code Playgroud)

要获取内存计数器列表:

Get-Counter -ListSet *memory* | Select-Object -ExpandProperty  Counter
Run Code Online (Sandbox Code Playgroud)

  • 为了获得纯值,我这样做: `(Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue` 和 `(Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue` 。 (2认同)

Ani*_*han 50

Get-WmiObject win32_processor | select LoadPercentage  |fl
Run Code Online (Sandbox Code Playgroud)

这为您提供了CPU负载.

(正如EBGreen建议的那样)编辑:

Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
Run Code Online (Sandbox Code Playgroud)

  • 试试这个:Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | 选择平均值 (7认同)
  • 感谢您跟进编辑Anirudh. (2认同)

Gre*_*ray 24

我使用以下PowerShell代码段来获取本地或远程系统的CPU使用率:

Get-Counter -ComputerName localhost '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| Sort-Object -Property cookedvalue -Descending| Select-Object -First 20| ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100).toString('P')}} -AutoSize
Run Code Online (Sandbox Code Playgroud)

相同的脚本,但使用行继续格式化:

Get-Counter -ComputerName localhost '\Process(*)\% Processor Time' `
    | Select-Object -ExpandProperty countersamples `
    | Select-Object -Property instancename, cookedvalue `
    | Sort-Object -Property cookedvalue -Descending | Select-Object -First 20 `
    | ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100).toString('P')}} -AutoSize
Run Code Online (Sandbox Code Playgroud)

在4核系统上,它将返回如下所示的结果:

InstanceName          CPU
------------          ---
_total                399.61 %
idle                  314.75 %
system                26.23 %
services              24.69 %
setpoint              15.43 %
dwm                   3.09 %
policy.client.invoker 3.09 %
imobilityservice      1.54 %
mcshield              1.54 %
hipsvc                1.54 %
svchost               1.54 %
stacsv64              1.54 %
wmiprvse              1.54 %
chrome                1.54 %
dbgsvc                1.54 %
sqlservr              0.00 %
wlidsvc               0.00 %
iastordatamgrsvc      0.00 %
intelmefwservice      0.00 %
lms                   0.00 %
Run Code Online (Sandbox Code Playgroud)

ComputerName参数将接受服务器列表,因此通过一些额外的格式化,您可以生成每个服务器上的顶级进程列表.就像是:

$psstats = Get-Counter -ComputerName utdev1,utdev2,utdev3 '\Process(*)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty countersamples | %{New-Object PSObject -Property @{ComputerName=$_.Path.Split('\')[2];Process=$_.instancename;CPUPct=("{0,4:N0}%" -f $_.Cookedvalue);CookedValue=$_.CookedValue}} | ?{$_.CookedValue -gt 0}| Sort-Object @{E='ComputerName'; A=$true },@{E='CookedValue'; D=$true },@{E='Process'; A=$true }
$psstats | ft @{E={"{0,25}" -f $_.Process};L="ProcessName"},CPUPct -AutoSize -GroupBy ComputerName -HideTableHeaders
Run Code Online (Sandbox Code Playgroud)

这将导致$ psstats变量与原始数据和以下显示:

   ComputerName: utdev1

           _total  397%
             idle  358%
             3mws   28%
           webcrs   10%


   ComputerName: utdev2

           _total  400%
             idle  248%
             cpfs   42%
             cpfs   36%
             cpfs   34%
          svchost   21%
         services   19%


   ComputerName: utdev3

           _total  200%
             idle  200%
Run Code Online (Sandbox Code Playgroud)


NoO*_*One 20

我已将上述所有答案组合到一个脚本中,该脚本轮询计数器并在终端中写入测量值:

$totalRam = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
while($true) {
    $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $cpuTime = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
    $availMem = (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue
    $date + ' > CPU: ' + $cpuTime.ToString("#,0.000") + '%, Avail. Mem.: ' + $availMem.ToString("N0") + 'MB (' + (104857600 * $availMem / $totalRam).ToString("#,0.0") + '%)'
    Start-Sleep -s 2
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下输出:

2020-02-01 10:56:55 > CPU: 0.797%, Avail. Mem.: 2,118MB (51.7%)
2020-02-01 10:56:59 > CPU: 0.447%, Avail. Mem.: 2,118MB (51.7%)
2020-02-01 10:57:03 > CPU: 0.089%, Avail. Mem.: 2,118MB (51.7%)
2020-02-01 10:57:07 > CPU: 0.000%, Avail. Mem.: 2,118MB (51.7%)
Run Code Online (Sandbox Code Playgroud)

您可以点击Ctrl+C以中止循环。

因此,您可以使用以下命令连接到任何 Windows 机器:

Enter-PSSession -ComputerName MyServerName -Credential MyUserName
Run Code Online (Sandbox Code Playgroud)

...粘贴它并运行它,以获得“实时”测量。如果无法直接连接到机器,请查看此处