I have this script and it works well for pulling this info 1 at a time for each cluster. I am trying to figure out how I can automate it so that I can pull all clusters in my vcenter at once, possibly creating a single spreadhseet with multiple tabs for each cluster. Any help would be greatly appreciated.
#########################################################
#
# PowerCLI Script to check Host Overcommitment (CPU & Memory)
#
#
#########################################################
# Select which vCenter you want to connect to
# Build array for each vCenter
Write-host "Select which vCenter to connect to:"
Write-Host ""
Write-Host "1. spvcenter11"
Write-Host "2. srvcenter11"
Write-Host "3. sdvcenter11"
$Ivcenter = read-host “Select a vCenter Server. Enter Number:“
if ($Ivcenter -match "1") {
$vcenter = "spvcenter11"
} elseif ($Ivcenter -match "2") {
$vcenter = "srvcenter11"
} else {
$vcenter = "sdvcenter11"
}
write-host ""
Write-Host "You Picked: $($vcenter)"
write-host ""
start-sleep -s 3
# connect to selected vCenter
connect-viserver $vcenter
# List Clusters
write-host ""
Write-host "Choose which Cluster you want to gather ratios on:"
write-host "(it may take a few seconds to build the list)"
write-host ""
$ICLUSTER = get-cluster | Select Name | Sort-object Name
$i = 1
$ICLUSTER | %{Write-Host $i":" $_.Name; $i++}
$HCLUSTER = Read-host "Enter the number for the host to Patch."
$SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name
write-host "You have selected $($SCLUSTER)."
start-sleep -s 3
# Get CPU Overcommitment Information
&{ForEach ($esx in (get-cluster $SCLUSTER | Get-VMHost)) {
$vCPU = Get-VM -Location $esx | where {$_.PowerState -match "on"} | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum
$esx | Select Name, @{N='pCPU cores available';E={$_.NumCpu}},
@{N='vCPU assigned to VMs';E={$vCPU}},
@{N='Ratio';E={[math]::Round($vCPU/$_.NumCpu,1)}},
@{N='CPU Overcommit (%)';E={[Math]::Round(100*(($vCPU - $_.NumCpu) / $_.NumCpu), 1)}}
# Export results to Excel
}} | Export-csv -path C:\OC\CPU_Overcommitment_$($SCLUSTER).csv
# Get Memory Overcommitment Information
&{ForEach ($esx in (get-cluster $SCLUSTER | Get-VMHost)) {
$vMem = get-vm -location $esx | where {$_.PowerState -match "on"} | measure-object -property MemoryGB -SUM | Select -Expandproperty Sum
$esx | Select Name, @{N='Total Memory Available';E={[Math]::Round($_.MemoryTotalGB),1}},
@{N='Memory Assigned to VMs';E={$vMem}},
@{N='Ratio';E={[math]::Round(100*($vMem / $_.MemoryTotalGB), 1)}},
@{N='Memory Overcommit (%)';E={[Math]::Round(100*(($vMem - $_.MemoryTotalGB) / $_.MemoryTotalGB), 1)}}
# Export results to CSV
}} | Export-csv -path C:\OC\memory_overcommitment_$($SCLUSTER).csv
# Disconnect from vCenter
Disconnect-viserver $vcenter -confirm:$false