Remote Check Memory Usage

Remote Check Memory Usage

When you have to do a lot of stuff on remote servers, the best you have, you MUST do is to check is the Ram usage is not too high.

Here is a little function that can help

Function Get-MemoryUsage {param($ComputerName)
$memory=Get-WmiObject -Class win32_operatingsystem -computername $ComputerName |
Select-Object @{Name = "MemoryUsage"; Expression = { “{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }
}
$memory=$memory.MemoryUsage
$memory=$memory.split(",")
$memory=$memory[0]
return $memory
}

On some powershell version (V3.0…) if the returned value is below 10, an error could happen if you try to compare that returned value with a number.

For example, an error I had encountered :

If($MemoryUsage -gt $MaxMemory){

Write-host "Over"

}

Over

I had to find another way to parse that error. I used the “length” property of the $MemoryUsage value.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site