Enregistrez les éléments suivants dans un fichier appelé mytop.ps1
dans un dossier de votre PATH
variable d’environnement. Utilisez ensuite l’un des éléments suivants à partir de n’importe quelle console PowerShell:
mytop
- utiliser le tri par défaut de la colonne 'Mémoire' et afficher les 30 premières lignes.
mytop CPU 50
- trier par la colonne 'CPU' et afficher les 50 premières lignes.
While(1) {$p = myTop Memory 50; cls; $p}
- de le rafraîchir toutes les secondes environ.
mytop.ps1
Contenu:
##################################################
# 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
Exemple de sortie:
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
Crédit supplémentaire à:
- rokumaru pour https://stackoverflow.com/a/55698377/5060792
- LotPings pour https://stackoverflow.com/a/55680398/5060792
- DBADon pour https://stackoverflow.com/a/55697007/5060792