Skip to content

Instantly share code, notes, and snippets.

@abix-
Last active August 29, 2015 14:25
Show Gist options
  • Save abix-/0e1ea0cc918dfa28653a to your computer and use it in GitHub Desktop.
Save abix-/0e1ea0cc918dfa28653a to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Collect configuration metrics for a VMHost, Cluster, Datacenter, or vCenter. For each VM Host found, these metrics are collected:
-Parent object(Cluster, Datacenter)
-Hardware model
-ESXi build
-IP address
-VMs on host
-physical CPUs(pCPU) on host
-virtual CPUs(vCPU) allocated
-vCPU to pCPU ratio
.DESCRIPTION
Connect to vCenter with Connect-VIServer before using this script.
Written with PowerShell v3.0 and PowerCLI 6.0(2548067).
.EXAMPLE
.\Get-VMHostMetrics -cluster "mycluster"
#>
[cmdletbinding()]
Param (
[Parameter(ParameterSetName="VMHost",Mandatory=$true,Position=0)]
[string]$vmhost,
[Parameter(ParameterSetName="Cluster",Mandatory=$true)]
[string]$cluster,
[Parameter(ParameterSetName="Datacenter",Mandatory=$true)]
[string]$datacenter,
[Parameter(ParameterSetName="vCenter",Mandatory=$true)]
[switch]$vcenter,
[switch]$report = $true,
[string]$reportName = "$($PSScriptRoot)\Reports\VMHostMetrics_"
)
function GetHostMetrics($h) {
#Determine configuration metrics
$view = $h | Get-View
$vms = $h | Get-VM
$totalCPU = ( $vms | Measure-Object -Sum -Property NumCPU).Sum
if(!$totalCPU) { $totalCPU = 0 }
$ratio = "{0:N3}" -f ($totalCPU/$h.NumCPU)
#Create an ordered PS Custom Object and store the metrics
$results = [pscustomobject][ordered]@{
VMHost = [string]$h.name
Parent = [string]$h.Parent
Model = [string]$h.Model
Build = [int]$view.Config.Product.Build
IPAddress = ($h | Get-VMHostNetwork).VirtualNic | Where-Object{$_.ManagementTrafficEnabled} | Select-Object -ExpandProperty IP
VMs = [int]$vms.count
pCPU = [int]$h.NumCPU
vCPU = [int]$totalCPU
Ratio = [double]$ratio
}
return $results
}
#Switch the Parameter Set and collect the appropriate VMware object
$all = @()
try {
switch($PSCmdlet.ParameterSetName) {
"VMHost" {
#Collect metrics from one host and append to $all
$h = Get-VMHost $vmhost -ErrorAction Stop; $all += GetHostMetrics $h
$reportName += $vmhost
}
"Cluster" {
#Loop through each host in the cluster, collect metrics, append to $all
$h = Get-Cluster $cluster -ErrorAction Stop | Get-VMHost | sort Name
$reportName += $cluster
foreach($_h in $h) { Write-Host "$cluster - $_h - Collecting data"; $all += GetHostMetrics $_h }
}
"Datacenter" {
#Loop thrugh each host in the datacenter, collect metrics, append to $all
$h = Get-Datacenter $datacenter -ErrorAction Stop | Get-VMHost | sort Name
$reportName += $datacenter
foreach($_h in $h) { Write-Host "$datacenter - $_h - Collecting data"; $all += GetHostMetrics $_h }
}
"vCenter" {
#Loop all hosts in vCenter, collect metrics, append to $all
$h = Get-VMHost -ErrorAction Stop | Sort Name
$reportName += "vCenter"
foreach($_h in $h) { Write-Host "$_h - Collecting data"; $all += GetHostMetrics $_h }
}
}
}
catch { Write-Host -ForegroundColor Red $_.Exception.Message; Exit }
$all | sort VMHost
#Export report
if($report -eq $true) {
$reportName += "_$(Get-Date -Format yyyyMMdd_HHmmss).csv"
$all | Export-csv $reportName -notype
Write-Host "Metrics have been written to $reportName"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment