Hello VMware and Powershell Experts,
I have created the below powercli script to collect inventory of our 5.5 and 6.0 VMware Hosts, VMs and Clusters. The problem with it is, it is terribly slow. It takes half a day to run this script fully to get details from 3 to 4 of our vCenters. Is there a way to make this script faster, by reducing the processing times, and cutting down unwanted loops, while maintaining the exact output, as requested by my manager? Input vcenter information at line 3 of the script. Thanks a million in advance!
$Username = Read-Host -Prompt "Please enter your Username:"
$Password = Read-Host -Prompt "Please enter your Password:"
$vcenters = @("vcenter1","vcenter2","vcenter3")
$GB = 1073718240
$HostReport = @()
$VMReport = @()
$ClusterReport = @()
ForEach ($vcenter in $vcenters)
{
Connect-VIServer $vcenter -User $Username -Password $Password
Get-VMHost |Get-View |%{
$HReport = "" | select vCenter, Hostname, Cluster, Stand_Alone, State, ESX_Version, Build, Model, CPU_Sockets, CPU_Cores, RAM_GB, VM
$HReport.vCenter = $vcenter
$HReport.Hostname = $_.Name
$HReport.Cluster = Get-Cluster -VMHost $_.Name
$HReport.Stand_Alone = if($HReport.Cluster) {'No'} else {'Yes'}
$HReport.State = $_.runtime.connectionState
$HReport.ESX_Version =$_.config.product.version
$HReport.Build =$_.config.product.build
$HReport.Model =$_.hardware.systemInfo.model
$HReport.CPU_Sockets =$_.hardware.CpuInfo.numCpuPackages
$HReport.CPU_Cores =$_.hardware.CpuInfo.numCpuCores
$HReport.RAM_GB = [math]::Round((($_.hardware.memorySize)/($GB)),1)
$HReport.VM = $_.vm.Count
$HostReport += $HReport
}
Get-VM |Get-View |%{
$VReport = "" | select vCenter, VM, IP, OS, ESX_Host, Status
$VReport.vCenter = $vcenter
$VReport.VM = $_.config.Name
$VReport.IP = $_.guest.ipaddress
$VReport.OS = $_.config.guestFullName
$VReport.ESX_Host = Get-VMHost -VM $_.config.Name
$VReport.Status = $_.guest.guestState
$VMReport += $VReport
}
Get-Cluster |Get-View |%{
$esx = Get-VMHost -Location $_.Name
$vm = Get-VM -Location $_.Name
$CReport = "" | select vCenter, DC, Cluster, Number_of_Hosts, Number_of_VMs, Total_Processors, Total_Cores, Total_Physical_Memory_GB, Configured_Memory_GB, Available_Memory_GB, Total_CPU_Ghz, Configured_CPU_Ghz, Available_CPU_Ghz
$CReport.vCenter = $vcenter
$CReport.DC = Get-Datacenter -Cluster $_.Name
$CReport.Number_of_Hosts = $esx.Count
$CReport.Number_of_VMs = $vm.Count
$CReport.Cluster = $_.Name
$CReport.Total_Processors = ($esx | measure -InputObject {$_.Extensiondata.Summary.Hardware.NumCpuPkgs} -Sum).Sum
$CReport.Total_Cores = ($esx | measure -InputObject {$_.Extensiondata.Summary.Hardware.NumCpuCores} -Sum).Sum
$CReport.Total_Physical_Memory_GB = [math]::Round((((($esx | Measure-Object -Property MemoryTotalMB -Sum).Sum)/1024)/($GB)),1)
$CReport.Configured_Memory_GB = [math]::Round((((($esx | Measure-Object -Property MemoryUsageMB -Sum).Sum)/1024)/($GB)),1)
$CReport.Available_Memory_GB = [math]::Round((((($esx | Measure-Object -InputObject {$_.MemoryTotalMB - $_.MemoryUsageMB} -Sum).Sum)/1024)/($GB)),1)
$CReport.Total_CPU_Ghz = [math]::Round((((($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum)/1024)/($GB)),1)
$CReport.Configured_CPU_Ghz = [math]::Round((((($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum)/1024)/($GB)),1)
$CReport.Available_CPU_Ghz = [math]::Round((((($esx | Measure-Object -InputObject {$_.CpuTotalMhz - $_.CpuUsageMhz} -Sum).Sum)/1024)/($GB)),1)
$ClusterReport += $CReport
$esx = ""
$vm = ""
}
Disconnect-VIServer $vcenter -Confirm:$false
}
$HostReport | Export-Csv "C:\HostReport.csv" -NoTypeInformation
$VMReport | Export-Csv "C:\VMReport.csv" -NoTypeInformation
$ClusterReport | Export-Csv "C:\ClusterReport.csv" -NoTypeInformation