0

I am running an instance of Windows Server 2012 R2 and would like to get the average memory usage of my server.

To get the CPU usage, I use

Get-WmiObject win32_processor | select LoadPercentage  |fl

and to get the average CPU usage, I have

Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

How do I do the same thing with memory usage?

3 Answers 3

4

You want the Win32_OperatingSystem namespace, and it's TotalVisibleMemorySize (physical memory), FreePhysicalMemory, TotalVirtualMemorySize, and FreeVirtualMemory properties.

Get-WmiObject win32_OperatingSystem |%{"Total Physical Memory: {0}KB`nFree Physical Memory : {1}KB`nTotal Virtual Memory : {2}KB`nFree Virtual Memory  : {3}KB" -f $_.totalvisiblememorysize, $_.freephysicalmemory, $_.totalvirtualmemorysize, $_.freevirtualmemory}

That will spit back:

Total Physical Memory: 4079572KB
Free Physical Memory : 994468KB
Total Virtual Memory : 8157280KB
Free Virtual Memory  : 3448916KB

I'm sure you can do the math if you want to get Used instead of Free.

Edit: Your CPULoad Average isn't really an average of anything. Case in point:

For($a=1;$a -lt 30;$a++){
    Get-WmiObject win32_processor|ForEach{
        [pscustomobject][ordered]@{
            'Average' = $_ | Measure-Object -property LoadPercentage -Average | Select -expand Average
            'Current' = $_ | select -expand LoadPercentage
        }
    }
    Start-Sleep -Milliseconds 50
}

Results:

Average                                          CPU Load
-------                                          --------
2                                                 2
1                                                 1
1                                                 1
1                                                 1
1                                                 1
5                                                 5
1                                                 1
1                                                 1
0                                                 0
1                                                 1
1                                                 1
1                                                 1
2                                                 2
4                                                 4
0                                                  
1                                                 1
7                                                 7
24                                                24
1                                                 1
Sign up to request clarification or add additional context in comments.

6 Comments

Is there anyway to get an average here like with CPU usage?
Average over time? Not that I know of. Of coarse, you aren't really getting an average over time with the CPU load either. You're getting the average of a single result of LoadPercent.
I am confused. How can it be an average if it's a single result?
It runs the Average formula against a single item array basically. So Sum(LoadPercent)/1 is essentially all it's doing.
Sorry to burst your bubble on that one.
|
0

If you want an average over a period of time you could use performance counters

### Get available memory in MB ###
$interval = 1 #seconds
$maxsamples = 5
$memorycounter = (Get-Counter "\Memory\Available MBytes" -maxsamples $maxsamples -sampleinterval $interval | 
select -expand countersamples | measure cookedvalue -average).average
### Memory Average Formatting ###
$freememavg = "{0:N0}" -f $memorycounter
### Get total Physical Memory & Calculate Percentage ###
$physicalmemory = (Get-WMIObject -class Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum / 1mb
$physicalmemory - $freememavg

Comments

0
Function Get-ADtop {
[CmdletBinding()]
param(
    [String]$ComputerName,
    [String]$Sort = "none",  
    [String]$BaseDN = "OU=systems,DC=domain,DC=com",  # Edit Default Base DN
    [String]$SampleTime = 2
)
If ($ComputerName) {
    $Computers = $ComputerName
} else {
    $Computers = Get-ADComputer -Filter * -Properties * -SearchBase $BaseDN -EA SilentlyContinue | % {$_.Name}
}
$DataSet = @()
$Targets = @()
ForEach ($Comp in $Computers) {
    If (Test-Connection -ComputerName $Comp -Count 1 -Quiet -TimeToLive 1 -EA SilentlyContinue) {
        If (!(Get-WmiObject -ComputerName $Comp win32_OperatingSystem -EA SilentlyContinue)) { break }
        $Targets += $Comp
    }
}
$CompCount = $Computers | Measure-Object | % {$_.Count}
$DeadCount = $CompCount - ($Targets | Measure-Object | % {$_.Count})
If (!($DeadCount -eq 0)) {
    Write-Host "`n$DeadCount unavailable computers removed"
}
Write-Host "`nGathering realtime CPU/MEM/DISK Usage data from $CompCount computers..."
ForEach ($Comp in $Targets) {
    $proc = (Get-WmiObject -ComputerName $Comp -class win32_processor -EA SilentlyContinue | Measure-Object -property LoadPercentage -Average | Select Average | % {$_.Average / 100}).ToString("P")
    $mem = Get-WmiObject -ComputerName $Comp win32_OperatingSystem -EA SilentlyContinue 
    $mem = (($mem.TotalVisibleMemorySize - $mem.FreePhysicalMemory) / $mem.TotalVisibleMemorySize).ToString("P")
    $disk = Get-WmiObject -ComputerName $Comp -class Win32_LogicalDisk -filter "DriveType=3" -EA SilentlyContinue 
    $disk = (($disk.Size - $disk.FreeSpace) / $disk.Size).ToString("P")
    $Info = [pscustomobject]@{
        'Computer' = $Comp
        'CPU Usage' = $proc
        'MEM Usage' = $mem
        'Disk Usage' = $disk
    }
    $DataSet += Add-Member -InputObject $Info -TypeName Computers.CPU.Usage -PassThru    
}
Switch ($Sort) {
    "none" { $DataSet }
    "CPU" { $DataSet | Sort-Object -Property "CPU Usage" -Descending }
    "MEM" { $DataSet | Sort-Object -Property "MEM Usage" -Descending }
    "DISK" { $DataSet | Sort-Object -Property "DISK Usage" -Descending }
}
}

More info here GitHub Gist Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.