Speichern Sie Folgendes in einer Datei mit dem Namen mytop.ps1
in einem Ordner, der sich in Ihrer PATH
Umgebungsvariablen befindet. Verwenden Sie dann eine der folgenden Optionen in einer beliebigen PowerShell-Konsole:
mytop
- Standardmäßig nach der Spalte 'Speicher' sortieren und die ersten 30 Zeilen anzeigen.
mytop CPU 50
- Nach der Spalte 'CPU' sortieren und die ersten 50 Zeilen anzeigen.
While(1) {$p = myTop Memory 50; cls; $p}
- um es jede Sekunde oder so aktualisieren zu lassen.
mytop.ps1
Inhalt:
##################################################
# Linux top equivalent in PowerShell
##################################################
if ($args[0] -eq $null) {
$SortCol = "Memory"
} else {
$SortCol = $args[0]
}
if ($args[1] -eq $null) {
$Top = 30
} else {
$Top = $args[1]
}
$LogicalProcessors = (Get-WmiObject -class Win32_processor `
-Property NumberOfLogicalProcessors).NumberOfLogicalProcessors;
function myTopFunc ([string]$SortCol = "Memory", [int]$Top = 30) {
## Check user level of PowerShell
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;
Description = $procTbl.Description[$procIndex];
Title = $procTbl.MainWindowTitle[$procIndex];
}
} |
sort -des $SortCol |
select -f $Top @(
"Name", "ID", "User",
@{ n = "CPU"; e = { ("{0:N1}%" -f $_.CPU) } },
@{ n = "Memory"; e = { ("{0:N0} K" -f $_.Memory) } },
"Description", "Title"
) | ft -a
}
myTopFunc -SortCol $SortCol -top $Top
Beispielausgabe:
Name ID User CPU Memory Description
---- -- ---- --- ------ -----------
sqlservr 7776 NT SERVICE\MSSQLSERVER 0.0% 19,001,488 K SQL Server Windows NT - 64 Bit
python 12872 NA\user1 0.0% 2,159,796 K Python
svchost 3328 NT AUTHORITY\SYSTEM 1.6% 1,022,080 K Host Process for Windows Services
onedrive 11872 NA\user1 0.0% 423,396 K Microsoft OneDrive
python 13764 NA\user1 0.0% 304,608 K Python
chrome 21188 NA\user1 0.0% 250,624 K Google Chrome
python 28144 NA\user2 0.0% 225,824 K Python
code 21384 NA\user1 0.0% 211,160 K Visual Studio Code
code 27412 NA\user2 0.0% 185,892 K Visual Studio Code
ssms 18288 NA\user1 29.5% 155,452 K SSMS
chrome 7536 NA\user1 0.0% 154,124 K Google Chrome
code 21652 NA\user1 0.0% 149,900 K Visual Studio Code
explorer 3204 NA\user1 0.0% 134,340 K Windows Explorer
python 11712 NA\user1 0.0% 130,624 K Python
chrome 21588 NA\user1 0.0% 107,448 K Google Chrome
code 10152 NA\user1 0.0% 100,880 K Visual Studio Code
code 20232 NA\user2 0.0% 99,124 K Visual Studio Code
python 22184 NA\user1 0.0% 94,800 K Python
code 14828 NA\user1 0.0% 84,872 K Visual Studio Code
searchui 13344 NA\user1 0.0% 78,260 K Search and Cortana application
com.docker.service 10644 NT AUTHORITY\SYSTEM 0.0% 77,332 K Docker.Service
Zusätzliches Guthaben für:
- rokumaru für https://stackoverflow.com/a/55698377/5060792
- LotPings für https://stackoverflow.com/a/55680398/5060792
- DBADon für https://stackoverflow.com/a/55697007/5060792