...hopefully some useful VMware related stuff

Display memory allocated and memory available across environment

20/05/2010 12:00

Script displays total memory available in each cluster, total memory allocated to VMs in each cluster and a percentage allocated. If over 100% message stating over-commitment is present. Then a table for each host in the cluster and same items as above is displayed.

 

The script can be run from a standard PowerShell window, as it checks for the VMware snapin and loads if necessary. If no connection exists to vCenter server it will prompt for name and credentials.

 

 

The script can be downloaded HERE and is shown below:

 

 

# List Memory Allocated out of total Physical Memory
# https://www.a2-alpha.co.uk
# 20100520

#Check for VMware PS Snapin - load and initialise if not

$erroractionpreference = "silentlycontinue"

$snapins = get-pssnapin | select Name
foreach($snapin in $snapins)
{
        if($snapin.Name -eq "VMware.VimAutomation.Core"){
        $result = "Yes"
    }
    else
    {
        $result = "No"
    }
}
if($result -eq "No")
{
    Add-PSsnapin VMware.VimAutomation.Core
    Initialize-VIToolkitEnvironment.ps1
}

#Test if connected to viserver or not

$Error.clear()

get-vm | out-null

$E = $Error
$E = $E | out-string
if($E.Contains("Get-VM"))
{
    $vcserver = read-host "Enter vCenter name or ESX host name"
    $us = read-host "Enter Username"
    $pw = read-host "Enter Password" -assecurestring:$true
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $us,$pw
    Connect-VIServer -Server $vcserver -Credential $cred | out-null
    [int]$connectionstate = 0
}

#Clear Screen ready for results

clear
" "
"** Memory Allocation and Memory Allocated to Physical Memory Available **"
" "

#Get all powered on VMs and get memory

$clusters = get-cluster | sort Name
foreach($cluster in $clusters)
{
"Cluster: $cluster"
" "
$vmon = get-vm -location $cluster | where {$_.PowerState -eq "PoweredOn"}
$vmoncount = $vmon.Count
$vmonmems = get-vm -location $cluster | where {$_.PowerState -eq "PoweredOn"} | select Name, MemoryMB
$memtotal = 0

#Run through each VM and count memory

foreach($vmonmem in $vmonmems)
{
      [int]$number = $vmonmem.MemoryMB
      $memtotal += $number
}
"$memtotal MB`t Total Memory assigned across all hosts"

#Get hosts then run through each to count total memory

$vmhosts = get-vmhost -location $cluster | get-view | sort Name
$memorytotal = 0
foreach($vmhost in $vmhosts)
{
      [int]$physmem = (($vmhost.Hardware.MemorySize)/1024/1024)
      $memorytotal += $physmem
}
"$memorytotal MB`t Total Physical memory available across all hosts"
$percallocated = [math]::round((([int]($memtotal)/[int]($memorytotal))*100),0)
"$percallocated % `t `t Memory allocated from total available"
" "
if($percallocated -gt [int](100))
{
    "The memory in this environment is Over Commited"
}

#Create table for memory allocation information within each host

$Report = @()
ForEach($vmhost in $vmhosts)
{
      $vmsrunningperhost = (Get-vmhost $VMHost.name | get-vm | where {$_.Powerstate -eq "PoweredON"})
      $runningperhostcount = (Get-vmhost $vmhost.name | get-vm | where {$_.Powerstate -eq "PoweredON"} | Measure-Object).Count
      $number = 0
      $memtotal = 0
      foreach($vm in $vmsrunningperhost)
      {
              [int]$number = $vm.MemoryMB
              $memtotal += $number
      }
      [int]$memtotal = $memtotal/1024
      $memorytotal = [math]::round(((($vmhost.Hardware.MemorySize)/1024/1024/1024)),0)
      $vmem2pmem = [math]::round((([int]($memtotal)/[int]($memorytotal))*100),1)

      $ReportObj = "" | Select "ESX Host", "Physical Memory Total", "Memory Allocated", "Allocated (%)", "Number of Running VMs"
      $ReportObj."ESX Host" = $vmhost.Name
      $ReportObj."Physical Memory Total" = $memorytotal
      $ReportObj."Memory Allocated" = $memtotal
      $ReportObj."Allocated (%)" = $vmem2pmem
      $ReportObj."Number of Running VMs" = $runningperhostcount
      $Report += $ReportObj
}
$Report | format-table
}
#Disconnect after results displayed if it was not connected when script was run

if($connectionstate -eq 0)
{
     disconnect-viserver -confirm:$false
}

Search site