Quick Host Memory Stats with PowerCLI

Greetings all! vNoob Here! I am going to be doing some blog posts for vBrownbag. If we haven’t met yet you can find me on twitter or on my website.

Anyway let’s get started.

One thing I noticed lately is there really isn’t an easy or quick way to check out what the current memory statistics are for hosts. Sure you could look at the Summary page for a single host.

But this only really tells you the “Consumed” memory, labeled and “usage” here, and the “Granted” memory, labeled as “Capacity”.

Or you could even look at the Performance Overview section for a Cluster.

But that also doesn’t really give you great insight on how each host is doing.

Yeah you could also setup a more advanced chart with each host and each stat you want, but that takes time, and you are only looking at one host at a time.

All of this to say, I wanted some better… so naturally I wrote a PowerCLI Script.

Function Get-HostMemory {

 

param([switch]$refresh,$hostname=”*”)

 

$vmhosts=Get-vmhost $hostname

Function get-memory{
$output=@()
ForEach($vmhost in $vmhosts)
{
$stat=get-stat -entity $vmhost -stat mem* -realtime
$Objects = New-Object PSObject -Property ([ordered]@{
Name=$vmhost.name
TotalGB=”{0:N2}” -f ($vmhost.memorytotalGB)
GrantedGB=$granted=”{0:N2}” -f (($stat |Where-Object{$_.metricid -like  “mem.granted.average”}|select -expand value -first 1)/1MB)
ConsumedGB=”{0:N2}” -f (($stat |Where-Object{$_.metricid -like “mem.consumed.average”}|select -expand value -first 1)/1MB)
ActiveGB=$active=”{0:N4}” -f (($stat |Where-Object{$_.metricid -like “mem.active.average”}|select -expand value -first 1)/1MB)
Usage =”{0:P0}” -f (($stat |Where-Object{$_.metricid -like “mem.usage.average”} |select -expan value -first 1)/100)
})
$objects|add-member –MemberType NoteProperty -Name UsageActive -value (“{0:P0}” -f ($active/$granted))
$output+= $objects
}
$output | Format-table -auto
}

IF ($refresh)
{
while(0 -ne 1)
{get-memory
sleep 15}
}
Else {Get-memory}

}

This script grabs a bunch of stats from all the hosts(or you can specify particular host) and returns to you the current info on each host.

As you can see, it organized each host on their own line, and presents the stats for that host.

Two quick things to note:

  1. “Usage” is VMware’s Usage, which is percent based off of Consumed to Granted
  2. “UsageActive”, is my own stat, which is based off ActiveGB to Consumed. I think this stat is a bit more meaningful.

You can also do a refresh and it will refresh the stats every 15 seconds

I would do the refresh sooner, but the stats themselves that I pull from only refresh every 15 seconds…

And finally you can grab the stats for just the host you specify

 

Download the the script here.

I am hoping this is helpful, and be sure to let me know what you think in the comments or hit me up on twitter.

3 thoughts on “Quick Host Memory Stats with PowerCLI

  • hi,

    just the script I need however I get the following when running it. what am I missing?

    PowerCLI D:VMware> .hostmemory.ps1

    Unexpected token ‘:P0’ in expression or statement.

    At D:VMwarehostmemory.ps1:46 char:82

    + $objects|add-member â?”MemberType NoteProperty -Name UsageActive -value (“{0:P0 <<<< }" -f ($active/$granted))

    + CategoryInfo : ParserError: (:P0:String) [], ParseException

    + FullyQualifiedErrorId : UnexpectedToken

  • in fact, I had an issue with a copy/paste of the script. I download the file and ran that instead and it worked. however I now have another issue. I am running this on powerCli 5.1 does this function work on that version.

    I get the following.

    Unable to find type [ordered]: make sure that the assembly containing this type is loaded.

    At line:30 char:60

    + $Objects = New-Object PSObject -Property ([ordered] <<<< @{

    + CategoryInfo : InvalidOperation: (ordered:String) [], RuntimeException

    + FullyQualifiedErrorId : TypeNotFound

    Bad numeric constant: 4,1546.

    At line:38 char:101

    + $objects|add-member -MemberType NoteProperty -Name UsageActive -value ("{0:P0}" -f ($active/ <<<< $granted))

    + CategoryInfo : InvalidOperation: (4,1546:String) [], RuntimeException

    + FullyQualifiedErrorId : BadNumericConstant

  • Hello,

    at my Client there is no Output. i am doing something wrong?

    Output:
    PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> %PathtoFile%test2.ps1
    Ende
    Thank you.

    Rolf

Comments are closed.