General vSphere Cluster counts/averages

A year or so ago, I had added some generic sections to Alan Renouf’s vCheck 5 script, which I was using to report on several environments that I managed. A few months ago, Alan released a new version of vCheck 6. I decided to re-write a couple of these checks into the new format. The new version of these checks are much more efficient than my previous versions, so I thought I would share them here.

Both checks make use of a $clusters variable, which can be created using the following line of code:

$clusters = Get-Cluster

This check will report on the average number of vCPUs per virtual machine and the average number of vCPUs per physical core.


$CpuClusterRatio = @()
$Clusters | select Name, ID | %{
	$thisClusterHost = Get-View -ViewType HostSystem -SearchRoot $_.id -property "Hardware.CpuInfo.NumCpuCores" | Select @{Name="NumCPU";Expression={$_.Hardware.CpuInfo.NumCpuCores}} |  Measure-Object -Property NumCPU -sum
	$thisClusterVM = Get-View -ViewType VirtualMachine -SearchRoot $_.id -property "Summary.Config" | Select @{Name="NumCPU";Expression={$_.Summary.Config.NumCPU}} |  Measure-Object -Property NumCPU -sum
	
	if ($thisClusterHost.Sum -gt 0) { $thisvCpuPerCore = ([math]::round(( $thisClusterVM.Sum / $thisClusterHost.Sum ), 2)) } else { $thisvCpuPerCore = 0 }
	if ($thisClusterVM.Count -gt 0) { $thisvCpuPerVM = ([math]::round(( $thisClusterVM.Sum / $thisClusterVM.Count ), 2)) } else { $thisvCpuPerVM = 0 }
	
	$CpuClusterRatio += New-Object psobject -Property @{
		Name = $_.Name
		NumHosts = $thisClusterHost.Count
		Cores = $thisClusterHost.Sum
		Guests = $thisClusterVM.Count
		"vCPU Count" = $thisClusterVM.Sum
		"vCPU per VM" = $thisvCpuPerVM
		"vCPU per Core" = $thisvCpuPerCore
	}
}

$CpuClusterRatio | select Name, NumHosts, Cores, Guests, "vCPU Count", "vCPU per VM", "vCPU per Core" | sort Name

This check will report on the average amount of RAM per virtual machine and the percentage of physical RAM allocated to all virtual machines.


$RamClusterRatio = @()
$Clusters | select Name, ID | %{
	$thisClusterHost = Get-View -ViewType HostSystem -SearchRoot $_.id -property "Hardware.MemorySize" | Select @{Name="RamSize";Expression={$_.Hardware.MemorySize/1GB}} |  Measure-Object -Property RamSize -sum
	$thisClusterVM = Get-View -ViewType VirtualMachine -SearchRoot $_.id -property "Summary.Config" | Select @{Name="RamSize";Expression={$_.Summary.Config.MemorySizeMB / 1024}} |  Measure-Object -Property RamSize -sum
	
	if ($thisClusterVM.count -gt 0) { $avgRamPerVM = ([math]::round(( $thisClusterVM.Sum / $thisClusterVM.Count ), 2)) } else { $avgRamPerVM = 0 }
	if ($thisClusterHost.sum -gt 0) { $allocatedRam = ([math]::round(( $thisClusterVM.Sum / $thisClusterHost.Sum ) * 100, 2)) } else { $allocatedRam = 0 }
	
	$RamClusterRatio += New-Object psobject -Property @{
		Name = $_.Name
		NumHosts = $thisClusterHost.Count
		"Cluster RAM (GB)" = [Math]::Round($thisClusterHost.Sum,0)
		Guests = $thisClusterVM.Count
		"Guest RAM (GB)" = [Math]::Round($thisClusterVM.Sum,2)
		"Avg RAM per VM" = $avgRamPerVM
		"Allocated RAM %" = $allocatedRam
	}
}

$RamClusterRatio | select Name, NumHosts, "Cluster RAM (GB)", Guests, "Guest RAM (GB)", "Avg RAM per VM", "Allocated RAM %" | sort Name

I hope someone can find a use for either of these scripts.

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

Leave a Reply

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