Monitoring memory usage with powershell

Hi

Recently I discovered that some of my managed servers has a memory leak.

to find which of the 300+ servers are running out of memory,

I wrote a simple and useful Powershell script.

You can use this as a template and add more hardware or software sampling with the correct WMI classes.

Download the Script from Here: Get-MemoryUsage.ps1

 

function Get-MemoryUsage ($ComputerName=$ENV:ComputerName) {

if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) {
$ComputerSystem = Get-WmiObject -ComputerName $ComputerName -Class Win32_operatingsystem -Property CSName, TotalVisibleMemorySize, FreePhysicalMemory
$MachineName = $ComputerSystem.CSName
$FreePhysicalMemory = ($ComputerSystem.FreePhysicalMemory) / (1mb)
$TotalVisibleMemorySize = ($ComputerSystem.TotalVisibleMemorySize) / (1mb)
$TotalVisibleMemorySizeR = “{0:N2}” -f $TotalVisibleMemorySize
$TotalFreeMemPerc = ($FreePhysicalMemory/$TotalVisibleMemorySize)*100
$TotalFreeMemPercR = “{0:N2}” -f $TotalFreeMemPerc

# print the machine details:
“Name: $MachineName”
“RAM: $TotalVisibleMemorySizeR GB”
“Free Physical Memory: $TotalFreeMemPercR %”

} }

Get-MemoryUsage

 

Old script:

$ErrorActionPreference = “silentlycontinue”

$a = Get-Content “C:\Scripting\servelist.txt”
foreach ($_ in $a)
{
Write-Host Monitoring $_
try
{
$TotMem = ((gwmi -Class win32_operatingsystem -computername $_).TotalVisibleMemorySize)/1kb
$FreeMem = ((gwmi -class win32_operatingsystem -computername $_).FreePhysicalMemory)/1kb
$TotFreeP = ($FreeMem/$TotMem)*100
$TotFree = “{0:N2}” -f $TotFreeP

if ($TotFreeP -lt 20)
{Write-Host server $_ has $totfree% of free memory }

# or you can send the results to a file with this line: {Write-Output  “server $_ has $totfree% of free memory ” | Out-File -append C:\Scripting\Results.txt }
catch
{
Write-Host Error monitoring server $_
}
}

Enjoy!

 

9 thoughts on “Monitoring memory usage with powershell”

      1. Just create a list of computers in a text file like this:
        server1
        server2
        server3

        name it serverslist.txt. place it in your C:\ drive.

        run this script:

        $list= Get-Content c:\serverslist.txt
        foreach ($i in $list){
        if(Test-Connection -ComputerName $i -Count 1 -Quiet) {
        $ComputerSystem = Get-WmiObject -ComputerName $i -Class Win32_operatingsystem -Property CSName, TotalVisibleMemorySize, FreePhysicalMemory
        $MachineName = $ComputerSystem.CSName
        $FreePhysicalMemory = ($ComputerSystem.FreePhysicalMemory) / (1mb)
        $TotalVisibleMemorySize = ($ComputerSystem.TotalVisibleMemorySize) / (1mb)
        $TotalVisibleMemorySizeR = “{0:N2}” -f $TotalVisibleMemorySize
        $TotalFreeMemPerc = ($FreePhysicalMemory/$TotalVisibleMemorySize)*100
        $TotalFreeMemPercR = “{0:N2}” -f $TotalFreeMemPerc

        # print the machine details:
        “Name: $MachineName”
        “RAM: $TotalVisibleMemorySizeR GB”
        “Free Physical Memory: $TotalFreeMemPercR %”

        } }

        1. Thank you Tamir.H, really great help from you and one more small request from you if possible can you please send me some of the useful power shell scripts for would be very much useful in production environment as i am very new to power shell so please help me in this regards to develop my skills in power shell

Leave a Reply