I am trying to get PowerShell to give me the RAM and CPU usage, but I can't figure out what WMI class to use. My computer has two processors, so it would be useful to have the information for both of them.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Get-WmiObject win32_processor | select LoadPercentage |fl
This gives you CPU load.
(As EBGreen suggests) EDIT:
Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
回答2:
You can also use the Get-Counter cmdlet (PowerShell 2.0):
Get-Counter '\Memory\Available MBytes'
Get-Counter '\Processor(_Total)\% Processor Time'
To get a list of memory counters:
Get-Counter -ListSet *memory* | Select-Object -ExpandProperty Counter
回答3:
I use the following PowerShell snippet to get CPU usage for local or remote systems:
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
Same script but formatted with line continuation:
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
On a 4 core system it will return results that look like this:
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 %
The ComputerName argument will accept a list of servers, so with a bit of extra formatting you can generate a list of top processes on each server. Something like:
$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
Which would result in a $psstats variable with the raw data and the following display:
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%
回答4:
To export the output to file on a continuous basis (here every five seconds) and save to a CSV file with the Unix date as the filename:
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
}
标签:
powershell