forked from ycrash/yc-data-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop.ps1
41 lines (40 loc) · 2.67 KB
/
top.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$LogicalProcessors = (Get-WmiObject -class Win32_processor -Property NumberOfLogicalProcessors).NumberOfLogicalProcessors;
$SortCol = "CPU"
$Top = 30
if (
([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
)
{
$procTbl = get-process -IncludeUserName | select ID, Name, UserName, Description, MainWindowTitle
} else {
$procTbl = get-process | select ID, Name, Description, MainWindowTitle
}
Get-Counter `
'\Process(*)\ID Process',`
'\Process(*)\% Processor Time',`
'\Process(*)\Working Set - Private'`
-ea SilentlyContinue |
foreach CounterSamples |
where InstanceName -notin "_total","memory compression" |
group { $_.Path.Split("\\")[3] } |
foreach {
$procIndex = [array]::indexof($procTbl.ID, [Int32]$_.Group[0].CookedValue)
[pscustomobject]@{
Name = $_.Group[0].InstanceName;
ID = $_.Group[0].CookedValue;
User = $procTbl.UserName[$procIndex]
CPU = if($_.Group[0].InstanceName -eq "idle") {
$_.Group[1].CookedValue / $LogicalProcessors
} else {
$_.Group[1].CookedValue
};
Memory = $_.Group[2].CookedValue / 1KB;
}
} |
sort -des $SortCol |
select -f $Top @(
"Name", "ID", "User",
@{ n = "CPU"; e = { ("{0:N1}%" -f $_.CPU) } },
@{ n = "Memory"; e = { ("{0:N0} K" -f $_.Memory) } }
) | ft -a