More

VMware PowerCLI – Get Cluster host status script

By Antti Hurme 05/02/2013 5 Comments 1 Min Read

Made a small script to keep up with the hosts in a cluster, to get a daily report of their usage, Virtual machine count, and other overview information. This script can be scheduled with Task scheduler if you want.

 

##############################################################
###
### VMHost status query script
### Version 0.2
### 21.1.2013
###
##############################################################
###
### CONFIGURATION
###
###
$export_path = "C:\Datacenter\ESXi_Host_Status"
$export_datef = get-date -format yyyy-MM-dd-H-mm
$login_user = ""
$login_pwd = ""
$login_hosts = " INSERT YOUR vCenter Server here"
##############################################################
### END
##############################################################
###Prepare variables
$end = get-date
###Check if we are connected to vCenter Server(s)
if($global:DefaultVIServers.Count -lt 1)
{
 Connect-VIServer $login_hosts -User $login_user -Password $login_pwd -AllLinked:$true
}
else
{
 echo "Already connected"
}
###Query all VMHosts
$result = Get-VMHost | select name,`
@{N="CpuUsageMhz"; E={[Math]::Round($_.CpuUsageMhz, 3)}}, @{N="CpuTotalMhz"; E={[Math]::Round($_.CpuTotalMhz, 3)}},`
@{N="CpuUsage%"; E={"{0:P0}" -f [Math]::Round($_.CpuUsageMhz/$_.CpuTotalMhz, 3)}},`
@{N="MemoryUsageGB"; E={[Math]::Round($_.MemoryUsageGB, 3)}}, @{N="MemoryTotalGB"; E={[Math]::Round($_.MemoryTotalGB, 3)}},`
@{N="MemoryUsage%"; E={"{0:P0}" -f [Math]::Round($_.MemoryUsageGB/$_.MemoryTotalGB, 3)}},`
@{N="VM Count"; E={$_.ExtensionData.vm.count}},`
@{N="Uptime"; E={(new-timespan $_.ExtensionData.Summary.Runtime.BootTime $end).days}},`
@{N="Overall Status"; E={$_.ExtensionData.OverallStatus}}, `
@{N="Configuration Status"; E={$_.ExtensionData.ConfigStatus}}`
| sort name
###Print out the result
$result | format-table -autosize | out-default
###Export the result to a CSV file
$result | export-csv -path "$export_path\ESXi-Host-Status-$export_datef.csv" -useculture
Written By

Who am I? | Linkedin

View All Articles
V
D
5 Comments
  1. Great little script! But.

    How would you modify this to show the averages over the last thirty days? I supposed I could dump the data to a database on a daily basis, then use SQL to get thirty day averages, but that would take the automation piece out of it.

    1. Antti Hurme says:

      Thank you! :) If you would like to get better statistics than what is provided by this script, then you would have to use the get-stat command. As this script returns the current statistics at the time of the script being run, it won’t give you anything more.

      There is a built-in function called get-stat which will do exactly what you want. Something like this could work:
      Get-Stat -Entity $vm -start (get-date).AddDays(-30) -finish (Get-Date)-MaxSamples 10000 -stat “cpu.usage.average”,”mem.usage.average”

      Theres some more information about get-stat in this vmware community topic and LucD has some nice examples in there as well. Link here.

  2. Helle says:

    Hey, great Script!!! It helps a lot.
    But i have one Problem with it: it dont show’s me any MEM Values…..

    1. Antti Hurme says:

      Sorry for the late reply! What version of powercli/vCenter are you using? I’m not 100% but could be that it doesn’t work for 4.x.

  3. Joe says:

    This is great – and all I needed was the UPTIME value. Question – if I have multiple vCenters, is there a way that the script can be configured to connect to each one ?

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.