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!
wow :O perfekt thats what i need <3333
Works perfect!! Thanks a lot
Can i get how to run it for 100 computers in my enviroment i can run in my local system that is fine but i need for remote computers that i am having 100 systems
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 %”
} }
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
Hi Shekhar
You can search intechnet
There are lots of good scripts there.
I will share any script that I think will be interesting.
None of these that I have came across seem to work.
what doesnt seems to work?
Thanks Tamir