Using PowerShell to Gather Quick and Easy ESXi Host Level Performance and Datastore Information

Many times when working with customers, we need to gather some quick specs around their existing environment to begin a sizing conversation. While there are many tools out there where you can let them run and capture the data over a period of time (eg: Dell’s DPACK Utility for an example), I needed something that a customer could run and capture some high-level performance information that’s already been collected in vCenter.

The script below can be used for that purpose. Just copy/paste into a new PowerShell script, edit the variables as needed, and then run. Three CSV files will be created in the output directory, which you can then open in excel, consolidate, make pivot tables, or whatever you’d like.

In the future, I hope to add more functionality to the script to collect additional information, consolidate the output into one file and a few other tweaks as time allows. Hope it’s helpful! 🙂

# AUTHOR
# Dennis Laube - dennis@nutanix.com - https://www.virtualdennis.com
#
# NAME
# gather-vm-perf-stats.ps1
#
# SYNOPSIS
#   Gathers ESXi Host CPU and RAM performance summaries and datastore summaries
#
# SYNTAX
#   C:\scriptlocation\gather-vm-perf-stats.ps1
# 
# DESCRIPTION
#   Will collect the CPU and RAM performance summaries listed by ESXi host along with a summary for all datastores. Output will be saved to 3 CSV files.
#   CSV files collected:
#   "gather-host-perf-stats.csv" = Overview of each host with CPU and RAM usage info
#   "gather-host-perf-stats-datastores.csv" = Overview of all datastores including usage info
#   "gather-host-perf-stats-hosthw.csv" = Overview of the hardware in each host, namely CPU type / Mhz / RAM installed
#
# REMARKS
#   If you don't have VMware Powershell Cmdlets installed, follow this article to install:
#   https://www.virtualdennis.com/installing-vmware-powercli-cmdlets-from-the-powershell-gallery/
#
#   vCenter needs to have "Statistics Level 2" enabled in order for the below to work
#   This can be enabled via the vSphere Client -> Administration -> vCenter Server Settings -> Statistics, and make
#   sure "Level 2" is under "Statistics Level" column
#
#   Adapted from Original CPU-RAM script: https://community.spiceworks.com/scripts/show/2632-powercli-get-host-info
#   Adapted Original Datastore Script provided by http://vniklas.djungeln.se/2012/05/08/powercli-report-on-datastores-overprovision-and-number-of-powered-on-vm%C2%B4s/
#   Thanks to LucD for a wealth of helpful blog posts at http://www.lucd.info/
#
# Main Variables (Edit As Needed)
#
# Enter the IP Address of your vCenter Server
$vcenter = "192.168.1.95"
# Enter the Domain Login information for your vCenter Server
$vcuser = "administrator@vsphere.local"
# Enter the password for the user above
$vcpwd = "Nutanix/4u"
# The final CSV file will be placed in this location (Default is directory called "Scripts" under C:)
$scriptlocation = "C:\Scripts"


#####################################
## No need to edit beyond this point
#####################################

# Connect to VC
Connect-VIServer $vcenter -User $vcuser -Password $vcpwd -ea silentlycontinue -WarningAction 0

# Add PowerCLI Snapin - not needed unless scheduling this script
#Add-PSSnapin VMware.VimAutomation.Core

# Get Host hardware information
Get-VMHost |Sort Name |Get-View |
Select Name, 
@{N=“Type“;E={$_.Hardware.SystemInfo.Vendor+ “ “ + $_.Hardware.SystemInfo.Model}},
@{N=“CPU“;E={“PROC:“ + $_.Hardware.CpuInfo.NumCpuPackages + “ CORES:“ + $_.Hardware.CpuInfo.NumCpuCores + “ MHZ: “ + [math]::round($_.Hardware.CpuInfo.Hz / 1000000, 0)}},
@{N=“MEM“;E={“” + [math]::round($_.Hardware.MemorySize / 1GB, 0) + “ GB“}} | Export-Csv $scriptlocation\gather-host-perf-stats-hosthw.csv -noTypeInformation

# Get CPU and RAM detailed info and usage per host 
$allhosts = @()
$hosts = Get-VMHost

foreach($vmHost in $hosts){
  $hoststat = "" | Select HostName, MemoryInstalled, MemoryAllocated, MemoryConsumed, MemoryUsage, CPUMax, CPUAvg, CPUMin
  $hoststat.HostName = $vmHost.name
  
  $statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average
  $statmemconsumed = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.consumed.average
  $statmemusage = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average
  $statmemallocated = Get-VMhost $vmHost.name | Select @{N="allocated";E={$_ | Get-VM | %{$_.MemoryGB} | Measure-Object -Sum | Select -ExpandProperty Sum}}
  $statmeminstalled = Get-VMHost $vmHost.name | select MemoryTotalGB
  $statmeminstalled = $statmeminstalled.MemoryTotalGB

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
  $memconsumed = $statmemconsumed | Measure-Object -Property value -Average
  $memusage = $statmemusage | Measure-Object -Property value -Average
  
  $CPUMax = "{0:N0}" -f ($cpu.Maximum)
  $CPUAvg = "{0:N0}" -f ($cpu.Average)
  $CPUMin = "{0:N0}" -f ($cpu.Minimum)
  $allocated = "{0:N0}" -f ($statmemallocated.allocated)
  $consumed = "{0:N0}" -f ($memconsumed.Average/1024/1024)
  $usage = "{0:P0}" -f ($memusage.Average/100)
  $installed = "{0:N0}" -f ($statmeminstalled)

  $CPUMax = $CPUMax.ToString() + " %"
  $CPUAvg = $CPUAvg.ToString() + " %"
  $CPUMin = $CPUMin.ToString() + " %"
  $MemoryInstalled = $installed.ToString() + " GB"
  $MemoryAllocated = $allocated.ToString() + " GB"
  $MemoryConsumed = $consumed.ToString() + " GB"
  $MemoryUsage = $usage.ToString()

  $hoststat.CPUMax = $CPUMax
  $hoststat.CPUAvg = $CPUAvg
  $hoststat.CPUMin = $CPUMin
  $hoststat.MemoryInstalled = $MemoryInstalled
  $hoststat.MemoryAllocated = $MemoryAllocated
  $hoststat.MemoryConsumed = $MemoryConsumed
  $hoststat.MemoryUsage = $MemoryUsage
  $allhosts += $hoststat
}
$allhosts | Select HostName, MemoryInstalled, MemoryAllocated, MemoryConsumed, MemoryUsage, CPUMax, CPUAvg, CPUMin | Export-Csv $scriptlocation\gather-host-perf-stats.csv -noTypeInformation

#Get Datastore Usage and save to CSV
Get-Datastore | Select Name,@{N="TotalSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},@{N="UsedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace)/1GB,0)}}, @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,0)}},@{N="NumVM";E={@($_ | Get-VM | where {$_.PowerState -eq "PoweredOn"}).Count}} | Sort Name | Export-Csv $scriptlocation\gather-host-perf-stats-datastores.csv -noTypeInformation

#Disconnect from current vCenter
Disconnect-VIServer $vcenter -Confirm:$false

Installing VMware PowerCLI Cmdlets from the PowerShell Gallery

A great writeup for this process with detailed information is located here:
https://blogs.vmware.com/PowerCLI/2017/04/powercli-install-process-powershell-gallery.html

In a nutshell:

Find-Module -Name VMware.PowerCLI

You might be prompted for a new version of “NuGet”, which is fine- go ahead and say “yes” to the prompt to install. Then proceed with the below.

Install-Module -Name VMware.PowerCLI -Scope CurrentUser

Once that completed, the VMware PowerCLI Cmdlets were installed.

However, when running

Connect-VIServer

I got the following error:
Import-Module : File \\vmware-host\Shared
Folders\Documents\WindowsPowerShell\Modules\VMware.VimAutomation.Sdk\1.0.0.5334677\VMware.VimAutomation.Sdk.ps1 cannot
be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.

So I went ahead and did the following:
– Launch the Windows PowerShell app using the “Run As Administrator” option.
Then type in:

Set-ExecutionPolicy RemoteSigned

Once that was done, then

Connect-VIServer

worked.

Configuring a VM to VM Anti-Affinity Rule on Nutanix AHV Running AOS 5.0

This how-to will show you how to configure a VM to VM anti-affinity rule on Nutanix AOS 5.0.

For reference, the official documentation on this feature is found at: https://portal.nutanix.com/#/page/docs/details?targetId=Web-Console-Guide-Prism-v50:ahv-ahv-vm-anti-affinity-t.html

Currently, on AOS 5.0, you can set a VM to Host affinity rule right within Prism by editing the VM properties. However, setting up a VM to VM anti-affinity rule will need to be done via the Acropolis CLI (ACLI). To configure VM to VM anti-affinity policies, you must first define a group and then add all the VMs on which you want to define the VM to VM anti-affinity policy.

Continue reading “Configuring a VM to VM Anti-Affinity Rule on Nutanix AHV Running AOS 5.0”

Patching VMware ESXi via Nutanix One-Click Upgrade in Prism on AOS 5.0

This lesson will show you how to patch VMware ESXi using the Nutanix one-click upgrade process in Prism.

If you have additional questions not covered in this how-to, please refer to the Nutanix Documentation Link for the one-click hypervisor upgrade feature, located here: https://portal.nutanix.com/#/page/docs/details?targetId=Web-Console-Guide-Prism-v50:wc-cluster-hypervisor-upgrade-esx-wc-t.html

Continue reading “Patching VMware ESXi via Nutanix One-Click Upgrade in Prism on AOS 5.0”

Registering vCenter with Prism on Nutanix AOS 5.0

To perform core VM management operations directly from Prism without switching to vCenter Server, you need to register your cluster with the vCenter Server. This how-to will show you how to register vCenter with Prism running Nutanix AOS 5.0.

By using this feature you can perform following virtual machine operations directly through Prism.

  • Create, clone, update, and delete VMs.
  • Create and delete NICs.
  • Attach and delete disks.
  • Power operations: Power on or off, reset, suspend, resume, guest shutdown, and guest reboot.
  • Open and launch VM console (via an HTML 5 web gui)
  • Manage VM guest tools (mounting VMware guest tools, mounting NGT).

Continue reading “Registering vCenter with Prism on Nutanix AOS 5.0”

Setting Up and Using Acropolis File Services (AFS) on Nutanix AOS 5.0

This article will show you the steps involved to setup Acropolis File Services (AFS) on a cluster running AOS 5.0.

Acropolis File Services (AFS) uses a scale-out architecture that provides Server Message Block (SMB) file shares to Windows clients for home directory and user profiles. Acropolis File Services consist of three or more file server VMs (FSVM). There is one file server maximum per cluster image. A set of file server VMs is also known as a Acropolis File Services cluster. Multiple file server clusters can be created on a Nutanix cluster.

With the AOS 5.0 release, Acropolis File Services can be used when running ESXi or AHV as the hypervisor on your Nutanix cluster.

A great document to refer to is the Acropolis File Services Guide, which can be found on the Nutanix portal here: https://portal.nutanix.com/#/page/docs/details?targetId=Acropolis-File-Services-Guide-v20:Acropolis-File-Services-Guide-v20

Continue reading “Setting Up and Using Acropolis File Services (AFS) on Nutanix AOS 5.0”

Nutanix PowerShell and Other Scripting Resources

Here are some helpful resources for scripting to the Nutanix API via PowerShell or other languages. I’ll continue to add more as I come across them. Feel free to leave your favorite resources in the comments section below! 🙂

https://portal.nutanix.com/#/page/docs/details?targetId=API_Ref-Acr_v4_5:PowerShell_Cmdlets_Reference
Cmdlets references with clickable links on each cmdlet that shows syntax

http://go.nutanix.com/rs/031-GVQ-112/images/Powershell_Automation_Poster.pdf
Cmdlet poster so you can decorate your cube with it 🙂 

http://nutanixbible.com/#anchor-powershell-cmdlets-38
Cmdlet “how to” on the Bible and some other examples

http://next.nutanix.com/t5/Scripts/bd-p/ScriptsGitHub
Scripts and other posts on the Community Forum 

https://github.com/nutanix
Github repository with scripts (some PowerShell, some others)