VMware Cloud Community
mekhan
Contributor
Contributor

Powercli help

Hi I have following powercli script to gather vcenter info. I need to add to get a list of hostnames, cluster name, folder name and VM name in it. Can someone help:

$hostinfo = @()
        ForEach ($vmhost in (Get-Cluster | Get-VMHost))
        {
            $HostView = $VMhost | Get-View
                        $HostSummary = "" | Select MemorySizeGB, CPUSockets, CPUCores, Version
                        $HostSummary.MemorySizeGB = $HostView.hardware.memorysize / 1024Mb
                        $HostSummary.CPUSockets = $HostView.hardware.cpuinfo.numCpuPackages
                        $HostSummary.CPUCores = $HostView.hardware.cpuinfo.numCpuCores
                        $HostSummary.Version = $HostView.Config.Product.Build
                        $hostinfo += $HostSummary
                    }

$vminfo = @()
            foreach($vm in (Get-Cluster | Get-VM))
        {
                $VMView = $vm | Get-View
                $VMSummary = "" | Select VMSockets,VMCores,CPUSockets,CPUCores,VMMem
                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu
                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket
                $VMSummary.VMMem = $VMView.Config.Hardware.MemoryMB

                $vminfo += $VMSummary
            }

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

And how would you combine that in 1 row?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mekhan
Contributor
Contributor

Thanks LucD to look at it. Script put the values in another variable, like:

$TotalHostRAM = ($hostinfo | Measure-Object -Property "MemorySizeGB" -Sum).Sum / 1024

$TotalVMRAM = ($vminfo | Measure-Object -Property "VMMem" -Sum).Sum / 1024 / 1024

Not sure if there is any other efficient way to achieve this goal.

Reply
0 Kudos
LucD
Leadership
Leadership

That's not what I mean, how would you combine all that information on 1 row?

Do you report for each VM, or each VMHost...?

How would an output row of the script look like?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mekhan
Contributor
Contributor

Data will feed to BI software. so separate list of hosts and separate list of virtual machines and other stuff will be good enough. At this moment I am not sure how BI software will massage the data but I think that should work.

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Have a look at this.

There is some redundancy in there, but it should capture the information you're after.

$report = foreach($vc in $global:defaultVIServers){

   foreach($cluster in Get-Cluster){

   foreach($vmhost in Get-VMHost -Location $cluster){

   foreach($vm in Get-VM -Location $vmhost){

   $obj = [ordered]@{

  vCenter = $vc.Name

  Cluster = $cluster.Name

  VMHost = $vmhost.Name

  Folder = $vm.Folder.Name

  VM = $vm.Name

  EsxMemorySizeGB = $vmhost.ExtensionData.hardware.memorysize / 1024Mb

  EsxCPUSockets = $vmhost.ExtensionData.hardware.cpuinfo.numCpuPackages

  EsxCPUCores = $vmhost.ExtensionData.hardware.cpuinfo.numCpuCores

  EsxVersion = $vmhost.ExtensionData.Config.Product.Build

  VMSockets = $vm.ExtensionData.Config.Hardware.NumCpu

  VMCores = $vm.ExtensionData.Config.Hardware.NumCoresPerSocket

  VMMem = $vm.ExtensionData.Config.Hardware.MemoryMB

  }

   New-Object PSObject -Property $obj

  }

  }

  }

}


$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos