【发布时间】:2021-09-18 23:13:16
【问题描述】:
我正在尝试让 PowerShell 为我提供 RAM 和 CPU 使用率,但我不知道要使用哪个 WMI 类。我的计算机有两个处理器,因此拥有这两个处理器的信息会很有用。
【问题讨论】:
标签: powershell
我正在尝试让 PowerShell 为我提供 RAM 和 CPU 使用率,但我不知道要使用哪个 WMI 类。我的计算机有两个处理器,因此拥有这两个处理器的信息会很有用。
【问题讨论】:
标签: powershell
您还可以使用 Get-Counter cmdlet (PowerShell 2.0):
Get-Counter '\Memory\Available MBytes'
Get-Counter '\Processor(_Total)\% Processor Time'
获取内存计数器列表:
Get-Counter -ListSet *memory* | Select-Object -ExpandProperty Counter
【讨论】:
(Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue 和 (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue。
$a = (Get-Counter "\Processor Information(_Total)\% Processor Performance", '\Memory\Available MBytes').CounterSamples.CookedValue,获取它$a[1]
Get-WmiObject Win32_Processor | Select LoadPercentage | Format-List
这会给你 CPU 负载。
Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Average
【讨论】:
我使用以下 PowerShell sn-p 来获取本地或远程系统的 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
相同的脚本,但格式为续行:
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
在 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 %
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
这将产生一个带有原始数据和以下显示的 $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%
【讨论】:
@(gwmi -ComputerName $server -Class Win32_ComputerSystemProcessor).Count 并添加 (gwmi Win32_Process -ComputerName $ComputerName -Credential $cred | Where {$_.ProcessId -eq $Process.IDProcess}).GetOwner().User?
我已将上述所有答案组合到一个脚本中,该脚本会轮询计数器并将测量结果写入终端:
$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
}
这会产生以下输出:
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%)
您可以点击Ctrl+C 中止循环。
因此,您可以使用以下命令连接到任何 Windows 机器:
Enter-PSSession -ComputerName MyServerName -Credential MyUserName
...粘贴并运行它,以获得“实时”测量。 如果无法直接连接机器,请查看here。
【讨论】:
连续输出输出到文件(这里是每五秒一次),并以 Unix 日期作为文件名保存到 CSV 文件中:
while ($true) {
[int]$date = get-date -Uformat %s
$exportlocation = New-Item -type file -path "c:\$date.csv"
Get-Counter -Counter "\Processor(_Total)\% Processor Time" | % {$_} | Out-File $exportlocation
start-sleep -s 5
}
【讨论】:
这是一种将最近 10 次测量值保存在注册表中并返回平均值的方法。它可能类似于每 6 小时运行一次的 Kace 自定义库存规则。我发现 CxUIUSvc 使用了很多 CPU。
if (test-path hkcu:\software\cpu) {$cpu = (get-itemproperty hkcu:\software\cpu).cpu}
$cpu = @($cpu + (Get-CimInstance win32_processor).loadpercentage)[-10..-1]
if (! (test-path hkcu:\software\cpu)) {new-item hkcu:\software\cpu > $null}
set-itemproperty hkcu:\software\cpu cpu $cpu -type multistring
($cpu | measure -average).average
使用调用命令检查历史记录:
$FormatEnumerationLimit = 10
icm comp1,comp2,comp3 { get-itemproperty hklm:\software\cpu -ea 0 | select cpu }
cpu PSComputerName RunspaceId
--- -------------- ----------
{2, 1, 2, 91, 7, 3, 0, 1} comp1 9b94ab81-2488-4322-bd8b-6c0afd53f340
{0, 99, 10, 4, 1, 1, 6, 5, 0, 3} comp2 02dbe3ed-c09d-430a-94f2-e55fe0b63b66
{0, 0, 13, 30, 57, 0, 20, 0, 0, 0} comp3 7f488a86-bb06-4b25-aa46-86dea603700d
【讨论】: