PowerCLI

 View Only
Expand all | Collapse all

Get VM, Host and Cluster info

  • 1.  Get VM, Host and Cluster info

    Posted Jun 19, 2020 08:45 PM

    Hi, I'm new to this and I'm looking for a script which pulls the following attributes and exports them to an excel.

    Input -> variable with all vCenters (list)

    Looking for

    VM Name, Host Name, Cluster Name, VM Power State, Host CPUs, Host CPU Type, Host Memory, Cluster Memory, HyperThread, VM Sockets, VM Cores Per Socket, Logical CPUs (VM Sockets * VM Cores Per Socket)

    I tried but was able to pull the Host related data in one script and VM related in other. I would like to combine both of these into one script, but was unsuccessful. Can someone please help me with this?

    Thanks, 
    Ganesh



  • 2.  RE: Get VM, Host and Cluster info

    Posted Jun 19, 2020 08:52 PM

    Can you share the scripts you already have?



  • 3.  RE: Get VM, Host and Cluster info

    Posted Jun 19, 2020 09:41 PM
    Thanks for the quick response.. Here you go
     
    $oStatsObjects = @()
    foreach($vm in Get-VM  | Where-object {$_.powerstate -eq "poweredon"})
    {
    # write-host $vm
    $esxHost = Get-VMHost -VM $vm
    $esxVM = Get-VM -Name $vm | Select-Object -Property Name,PowerState,NumCPU,NumCoresPerSocket,MemoryGB
    $oObject = New-Object PSObject -Property @{
    HostName = $esxHost.Name
    Cluster = $esxHost.Parent
    HostState = $esxHost.PowerState
    HostCPUs = $esxHost.NumCpu
    CPUType = $esxHost.ProcessorType
    Memory = $esxHost.MemoryTotalMB
    HyperThread = $esxHost.HyperthreadingActive
    VMName = $esxVM.Name
    VMState = $esxVM.PowerState
    VMSockets = $esxVM.NumCPU
    VMCoresPerSocket = $esxVM.config.hardware.NumCoresPerSocket
    VMLogicalCPUs = $esxVM.config.hardware.NumCPU * $esxVM.config.hardware.NumCoresPerSocket
    VMTotalMemory = $esxVM.MemoryGB
     
    write-host  "Exporting Data for Host - VM : $HostName - $vm"
    $oStatsObjects += $oObject
    $oObject = $null
    }


  • 4.  RE: Get VM, Host and Cluster info

    Posted Jun 19, 2020 10:10 PM

    Looks like I was able to get the solution

    $oStatsObjects = @()
    foreach($vm in $vms)
    {
    write-host $vm
    $esxHost = Get-VMHost -VM $vm
    $esxVM = Get-VM -Name $vm | Select-Object -Property Name,PowerState,NumCPU,NumCoresPerSocket,MemoryGB
    $esxVMCoresPerSocket = Get-View -ViewType VirtualMachine -filter @{"Name"=$vm} | Select-Object -Property config
    write-host $esxVM
    $oObject = New-Object PSObject -Property @{
    HostName = $esxHost.Name
    Cluster = $esxHost.Parent
    HostState = $esxHost.PowerState
    HostCPUs = $esxHost.NumCpu
    CPUType = $esxHost.ProcessorType
    Memory = $esxHost.MemoryTotalMB
    HyperThread = $esxHost.HyperthreadingActive
    VMName = $esxVM.Name
    VMState = $esxVM.PowerState
    VMSockets = $esxVM.NumCPU
    VMCoresPerSocket = $esxVMCoresPerSocket.config.hardware.NumCoresPerSocket
    VMLogicalCPUs = $esxVM.NumCPU * $esxVMCoresPerSocket.config.hardware.NumCoresPerSocket
    VMTotalMemory = $esxVM.MemoryGB
     
    $oStatsObjects += $oObject
    $oObject = $null
    }
     
    $csvExport = $oStatsObjects | ConvertTo-Csv -outvariable $csvOut -notypeinformation
    $csvExport[0..($csvExport.count -1)] | foreach-object {add-content -value $_ -path $outFileName}
    $oStatsObjects=$null


  • 5.  RE: Get VM, Host and Cluster info

    Posted Jun 19, 2020 10:38 PM

    However, I noticed that "VMCoresPerSocket" is returning "System.Object[]" for few. Can you please help me on how to handle this? Thanks in advance!

    VMCoresPerSocket = $esxVMCoresPerSocket.config.hardware.NumCoresPerSocket



  • 6.  RE: Get VM, Host and Cluster info

    Posted Jun 20, 2020 05:57 AM

    That could mean that more than one VM is returned on the Get-View cmdlet.

    Remember that the right-hand operator for an entry in the Filter hash table, is a RegExexpression.

    If, for example, the value in $vm is 'VM', it will return the VM with the name 'VM', but also 'VM1' and 'MyVM'.

    By using RegEx anchors you can filter on an exact match.

    $esxVMCoresPerSocket = Get-View -ViewType VirtualMachine -filter @{'Name'="^$vm$"} | Select-Object -Property config



  • 7.  RE: Get VM, Host and Cluster info

    Posted Jun 20, 2020 11:42 AM
    Thanks a lot for the explanation! I will give it a try and will update you here. Once again thank you so much for the help!


  • 8.  RE: Get VM, Host and Cluster info

    Posted Jun 20, 2020 11:55 AM
    Perfect! It is working as expected now. Thank you once again!


  • 9.  RE: Get VM, Host and Cluster info

    Posted Jun 21, 2020 03:46 AM

    I have around 16 vCenters and 15K VMs in total. This script takes forever to run and is very slow. Is there any better way to pull the data quickly? Please advise.



  • 10.  RE: Get VM, Host and Cluster info

    Posted Jun 21, 2020 07:45 AM

    There are a few options to improve the execution time.

    - use Get-View and EXtensionData

    - use the vSphere API directly instead of the PowerCLI cmdlets

    - run multiple background job to spread the load



  • 11.  RE: Get VM, Host and Cluster info

    Posted Jun 21, 2020 02:03 PM

    Thanks for the reply! We use Vmware version 6. I remember reading that Version 6 doesn't support APIs to pull this data. Is this true? If not, can you please share the API link that I can read through to get this done through APIs instead of PowerCLI cmdlets. Thanks in advance!



  • 12.  RE: Get VM, Host and Cluster info

    Posted Jun 21, 2020 03:26 PM

    I don't know where you heard that, but afaik you can use the API methods and properties.

    Since your script is accessing multiple properties from different objects, you will have to look at the API Reference to find the corresponding properties.



  • 13.  RE: Get VM, Host and Cluster info

    Posted Jun 21, 2020 03:54 PM

    Okay. Thank you! Will give it a read! Thank you once again for all the help! Really Appreciate!



  • 14.  RE: Get VM, Host and Cluster info

    Posted Jun 21, 2020 11:38 PM

    I just realized that we have few 5.5 vCenters and I tried and I did hit the roadblock for the vCenter version 5.5. When I try to get the cluster details, I get the following message.

    {"name":"com.vmware.vapi.rest.httpNotFound","localizableMessages":[{"defaultMessage":"Not found.","id":"com.vmware.vapi.rest.httpNotFound"}],"majorErrorCode":404}

    The same thing worked for another vCenter, which I believe is v6.

    Request : https://' + svCenterName + '/rest/vcenter/cluster'

    Can you please let me know if there is anything that I'm missing here?



  • 15.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 06:51 AM

    Yes, 5.5 doesn't have those REST API.



  • 16.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 04:08 PM

    okay got it. I got mixed up with the versions earlier.. My bad.. Can you help me on how to use the Get-View ExtensionData to speed up the process?



  • 17.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 04:22 PM

    I'm wondering why you were using a REST API.

    Which properties do you want to retrieve via the API?



  • 18.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 04:42 PM

    We have around 15K VMs across different vCenters. We need to pull the Cluster, Host, VM, CPU, SocketsPerCore, MemGB, PowerStatus, HyperThread. (Have shared my script earlier in this mail thread). Using the PowerCLI cmdlets takes hours to pull this data and when I asked on how to speed up this process, you have suggested to use the APIs.

    So, when I tried to use the APIs I have hit the roadblock as few vCenters are 5.5 version.

    Now, I want to go back to PowerCLI cmdlets to pull this data but want to speed up the process and not wait for hours to get this data.

    Can you guide me on how to get this data pulled quickly?

    Thanks in advance!



  • 19.  RE: Get VM, Host and Cluster info
    Best Answer

    Posted Jun 22, 2020 06:31 PM

    When I mentioned API I was referring to the SOAP API, not the REST API.

    In any case, try the following

    $sCluster = @{

        ViewType = 'ClusterComputeResource'

        Property = 'Name'

        PipelineVariable = 'cluster'

    }

    $sVMHost = @{

        ViewType = 'HostSystem'

        Property = 'Name','Runtime.PowerState','Summary.Hardware'

        PipelineVariable = 'esx'

    }

    $sVM = @{

        ViewType = 'VirtualMachine'

        Property = 'Name','Summary.Runtime.PowerState','Config.Hardware'

        PipelineVariable = 'vm'

        Filter = @{'Summary.Config.Template'='False'}

    }

    Get-View @sCluster |

    ForEach-Object -Process {

        Get-View @sVMHost -SearchRoot $cluster.MoRef |

        ForEach-Object -Process {

            Get-View @sVM -SearchRoot $esx.MoRef  |

            ForEach-Object -Process {

                $vm | Select @{N='Cluster';E={$cluster.Name}},

                    @{N='VMHost';E={$esx.Name}},

                    @{N='HostState';E={$esx.RunTime.PowerState}},

                    @{N='HostCPU';E={$esx.Summary.Hardware.NumCpuCores}},

                    @{N='CPUType';E={$esx.Summary.Hardware.CpuModel}},

                    @{N='MemoryMB';E={[math]::Round($esx.Summary.Hardware.MemorySize/1KB)}},

                    @{N='HyperThread';E={-not ($esx.Summary.Hardware.NumCpuCores -eq $esx.Summary.Hardware.NumCpuThreads)}},

                    @{N='VM';E={$vm.Name}},

                    @{N='VMState';E={$vm.Summary.Runtime.PowerState}},

                    @{N='VMSockets';E={$vm.Config.Hardware.NumCpu/$vm.Config.Hardware.NumCoresPerSocket}},

                    @{N='VMCoresPerSocket';E={$vm.Config.Hardware.NumCoresPerSocket}},

                    @{N='VMLogicalCPUs';E={$vm.Config.Hardware.NumCpu}},

                    @{N='VMTotalMemoryGB';E={$vm.Config.Hardware.MemoryMB/1KB}}

            }

        }

    }



  • 20.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 08:47 PM

    Thank you so much! It works fine. I added up the code to export to Excel as well.

    The value that is returned for MemorySize is in Bytes and as we are dividing it by 1024 the value is in KB. So, changed the label to MemoryKB instead of MB.

    @{N='MemoryKB';E={[math]::Round($esx.Summary.Hardware.MemorySize/1KB)}},



  • 21.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 08:56 PM

    What is the execution time like?
    I thought that was a critical point.



  • 22.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 09:42 PM

    It took less than 5 mins :smileyhappy:

    Earlier with my code, it took hours :smileyhappy:

    Thank you for all the help!

    How do I mark it as answered?



  • 23.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 09:59 PM

    Great!

    If you access the thread from the VMTN forum, you should see a Correct Answer button under replies.

    Re: Get VM, Host and Cluster info



  • 24.  RE: Get VM, Host and Cluster info

    Posted Jun 22, 2020 11:39 PM

    Done. Thank you!



  • 25.  RE: Get VM, Host and Cluster info

    Posted Jun 23, 2020 09:07 PM

    LucD​, Looks like there is something wrong with the mapping of cluster to the Host and VM details. I have noticed that Cluster information for few rows is completely incorrect.



  • 26.  RE: Get VM, Host and Cluster info

    Posted Jun 24, 2020 03:52 AM

    In what sense?
    Can you give an example?



  • 27.  RE: Get VM, Host and Cluster info

    Posted Jun 24, 2020 04:40 AM

    Host for Cluster A is showing up under Cluster B in the output file. I believe it is the way I was writing to the file.

    Can you please add the code to write to excel? I just want to compare my code with yours.

    Thanks,

    Ganesh



  • 28.  RE: Get VM, Host and Cluster info

    Posted Jun 24, 2020 04:54 AM

    Change the last line to something like this

    } | Export-Excel -Path .\report.xlsx -WorksheetName Report -TitleBold -AutoSize -FreezeTopRow -AutoFilter -Show



  • 29.  RE: Get VM, Host and Cluster info

    Posted Jun 24, 2020 04:55 AM

    Okay. Thank you! Will change that tomorrow and will keep you posted.

    Thanks once again for all the help!



  • 30.  RE: Get VM, Host and Cluster info

    Posted Jun 24, 2020 03:09 PM

    @LucD, I tried but it is still the same. So, I have modified the code to call each vCenter at a time rather than connecting to all of them together by placing the connect server code with in the for loop and it is working as expected.

    Thank you once again for all your help!



  • 31.  RE: Get VM, Host and Cluster info

    Posted Jun 25, 2020 06:57 AM

    Thanks LucD for sharing the script, I am trying to understand the logic but finding it hare. If possible can you put some comments to the code.. please.. Thanks.



  • 32.  RE: Get VM, Host and Cluster info

    Posted Jun 25, 2020 07:37 AM

    Does this help?
    Let me know if there are other aspects of the script that are unclear.

    # For the Get-View calls later in the script, I use 'splatting'

    # All cmdlet parameters are placed in a hash table

    #

    # The exception is the SearchRoot parameter, the variable used on that parameter

    # is not yet initialised at this point


    # The Get-View cmdlet allows one to specify which properties to fecth for an object

    # instead of retrieving the complete object.

    # This shortens the execution time of the cmdlet


    $sCluster = @{

        ViewType = 'ClusterComputeResource'

        Property = 'Name'

        PipelineVariable = 'cluster'

    }

    $sVMHost = @{

        ViewType = 'HostSystem'

        Property = 'Name','Runtime.PowerState','Summary.Hardware'

        PipelineVariable = 'esx'

    }

    $sVM = @{

        ViewType = 'VirtualMachine'

        Property = 'Name','Summary.Runtime.PowerState','Config.Hardware'

        PipelineVariable = 'vm'

        Filter = @{'Summary.Config.Template'='False'}

    }


    # The script uses 3 nested foreach loops

    # Cluster-VMHost-VM


    Get-View @sCluster |

    ForEach-Object -Process {

        # The SearchRoot parameter will only return objects that are under the entity specified on this parameter

        Get-View @sVMHost -SearchRoot $cluster.MoRef |

        ForEach-Object -Process {

            Get-View @sVM -SearchRoot $esx.MoRef  |

            ForEach-Object -Process {

                # The script uses Get-View and hence has to deal with vSphere objects

                # These objects, and their properties, are described in the API Reference

                # https://code.vmware.com/apis/968/vsphere

              

                $vm | Select @{N='Cluster';E={$cluster.Name}},

                    @{N='VMHost';E={$esx.Name}},

                    @{N='HostState';E={$esx.RunTime.PowerState}},

                    @{N='HostCPU';E={$esx.Summary.Hardware.NumCpuCores}},

                    @{N='CPUType';E={$esx.Summary.Hardware.CpuModel}},

                    @{N='MemoryMB';E={[math]::Round($esx.Summary.Hardware.MemorySize/1KB)}},

                    @{N='HyperThread';E={-not ($esx.Summary.Hardware.NumCpuCores -eq $esx.Summary.Hardware.NumCpuThreads)}},

                    @{N='VM';E={$vm.Name}},

                    @{N='VMState';E={$vm.Summary.Runtime.PowerState}},

                    @{N='VMSockets';E={$vm.Config.Hardware.NumCpu/$vm.Config.Hardware.NumCoresPerSocket}},

                    @{N='VMCoresPerSocket';E={$vm.Config.Hardware.NumCoresPerSocket}},

                    @{N='VMLogicalCPUs';E={$vm.Config.Hardware.NumCpu}},

                    @{N='VMTotalMemoryGB';E={$vm.Config.Hardware.MemoryMB/1KB}}

            }

        }

    }



  • 33.  RE: Get VM, Host and Cluster info

    Posted Jun 25, 2020 08:42 AM

    Thank's LucD, it helps but I need to do some research about working with API directly instead of Powercli cmdlets,  before I come and trouble you again. I know there is lot of material available online, but do you have anything in particular to suggest for beginners to intermediate level.



  • 34.  RE: Get VM, Host and Cluster info

    Posted Jun 25, 2020 08:52 AM


  • 35.  RE: Get VM, Host and Cluster info

    Posted Jul 06, 2020 11:59 PM

    Hi Luc,

    Great Script. How can we add to it the IP address of the VMs as well?

    Thanks,

    Hicham



  • 36.  RE: Get VM, Host and Cluster info

    Posted Jul 07, 2020 06:41 AM

    When you have the VMware Tools installed on the VMs, you can add the following calculated property.

    @{N='IP';E={$_.Guest.Net.IPConfig.IPAddress.IPAddress -join '|'}}

    Note that you will have to adapt the $sVM hash table as well

    $sVM = @{

        ViewType = 'VirtualMachine'

        Property = 'Name','Summary.Runtime.PowerState','Config.Hardware','Guest.Net'

        PipelineVariable = 'vm'

        Filter = @{'Summary.Config.Template'='False'}

    }



  • 37.  RE: Get VM, Host and Cluster info

    Posted Jul 07, 2020 03:12 PM

    Excellent!

    it worked and it is very fast too.

    Thanks, Luc!