Skip to content

Instantly share code, notes, and snippets.

@tadcrazio
Last active May 29, 2017 23:47
Show Gist options
  • Save tadcrazio/5eba6d5c2bcec4cf7d7a505d80a6bd28 to your computer and use it in GitHub Desktop.
Save tadcrazio/5eba6d5c2bcec4cf7d7a505d80a6bd28 to your computer and use it in GitHub Desktop.
Get Memory usage and top processes.
## 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