This one comes from my coworkers Mark Norstedt and Joseph Chanson. They put together a nice function that works similar to ‘ps’, but it can be run against a remote machine and the output it gives is a little more closely aligned with Task Manager. This is a real nice function to have when a remote machine is reporting a problem that looks like a wild process consuming the entire CPU, but you don’t want to put the extra burden on the machine of RDP’ing into it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ProcessUtilization | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[Alias('hostname')] | |
[Alias('cn')] | |
[Alias('ServerName')] | |
[string[]] $ComputerName = $env:COMPUTERNAME, | |
[Parameter(Position=2)] | |
[int] $SelectFirst = 10, | |
[Parameter(Position = 3)] | |
[string] $ProcessName = "*", | |
[Parameter(Position = 4)] | |
[string] $PID = "", | |
[Parameter(Position = 5)] | |
[ValidateSet("CPU","RAM")] | |
[string] $Sort = "CPU" | |
) | |
begin {} | |
process | |
{ | |
switch($Sort) { | |
"CPU" { $sortgwmi = "PercentProcessorTime" } | |
"RAM" { $sortgwmi = "WorkingSetPrivate" } | |
} | |
$processList = gwmi Win32_PerfFormattedData_PerfProc_Process -ComputerName $ComputerName | |
if ($ProcessName -ne "*") { | |
$processList = $processList | where { $_.Name -eq $ProcessName } | |
} | |
if ($PID -ne "") { | |
$processList = $processList | where { $_.IDProcess -eq $PID } | |
} | |
$processList = $processList | | |
select IDProcess, Name, PercentProcessorTime, WorkingSetPrivate | | |
where { $_.Name -ne "_Total" -and $_.Name -ne "Idle" } | | |
sort -Property $sortgwmi -Descending | | |
select -First $SelectFirst | |
$processes = @() | |
ForEach ($process in $processList) { | |
$properties = @{ | |
PID = $process.IDProcess | |
Name = $process.Name | |
User = (gwmi Win32_Process -ComputerName $ComputerName | where {$_.ProcessId -eq $process.IDProcess}).GetOwner().User | |
CPU = $process.PercentProcessorTime | |
RAM = $process.WorkingSetPrivate / 1KB | |
Description = (Get-Process -ComputerName $ComputerName -Id $process.IDProcess).Description | |
} | |
$cpuinfo = New-PSType -PsType "CoreUcsb.CpuUtilization" -Property $properties | |
$processes += @($cpuinfo) | |
} | |
return $processes; | |
$results = $processes | | |
sort -Property $Sort -Descending | | |
select PID, Name, User, CPU, RAM, Description | |
return $results | |
if ($GridView) { | |
$results | Out-GridView | |
} else { | |
$resultsFormat = @( | |
@{n="PID";e={$_.PID};a="left";w=6}, | |
@{n="Name";e={$_.Name};w=17}, | |
@{n="User";e={$_.User};w=20}, | |
@{n="CPU";e={$_.CPU};w=20}, | |
@{n="RAM";e={"{0:N0} K" -f $_.RAM};align="right";w=20} | |
@{n="Description";e={$_.Description};w=20} | |
) | |
$results | ft $resultsFormat -AutoSize | |
} | |
} | |
end {} | |
} |
0 comments:
Post a Comment