Last active
May 29, 2017 23:47
-
-
Save tadcrazio/5eba6d5c2bcec4cf7d7a505d80a6bd28 to your computer and use it in GitHub Desktop.
Get Memory usage and top processes.
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
## The idea and most of the code comes from. https://www.petri.com/display-memory-usage-powershell | |
Function show-MemoryUsage | |
{ | |
[cmdletbinding()] | |
Param() | |
$os = Get-Ciminstance Win32_OperatingSystem | |
$pctFree = [math]::Round(($os.FreePhysicalMemory / $os.TotalVisibleMemorySize) * 100, 2) | |
if ($pctFree -ge 45) | |
{ | |
$Status = "OK" | |
} | |
elseif ($pctFree -ge 15 ) | |
{ | |
$Status = "Warning" | |
} | |
else | |
{ | |
$Status = "Critical" | |
} | |
$data = $os | Select @{Name = "Status"; Expression = {$Status}}, | |
@{Name = "PctFree"; Expression = {$pctFree}}, | |
@{Name = "FreeGB"; Expression = {[math]::Round($_.FreePhysicalMemory / 1mb, 2)}}, | |
@{Name = "TotalGB"; Expression = {[int]($_.TotalVisibleMemorySize / 1mb)}} | |
$data2 = get-wmiobject WIN32_PROCESS | Sort-Object -Property ws -Descending|select -first 5|Select processname, @{Name = "Mem Usage(MB)"; Expression = {[math]::round($_.ws / 1mb)}}, @{Name = "ProcessID"; Expression = {[String]$_.ProcessID}} | |
$dataString = $data |Format-Table -AutoSize | Out-String | |
$data2string = $data2 |Format-Table -AutoSize| Out-String | |
$dataString | |
$data2string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment