VMware Cloud Community
ganesha31
Enthusiast
Enthusiast
Jump to solution

Get VM, Host and Cluster info

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

36 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
ganesha31
Enthusiast
Enthusiast
Jump to solution

It took less than 5 mins Smiley Happy

Earlier with my code, it took hours Smiley Happy

Thank you for all the help!

How do I mark it as answered?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
ganesha31
Enthusiast
Enthusiast
Jump to solution

Done. Thank you!

ganesha31
Enthusiast
Enthusiast
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In what sense?
Can you give an example?


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

0 Kudos
ganesha31
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Change the last line to something like this

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


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

ganesha31
Enthusiast
Enthusiast
Jump to solution

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

Thanks once again for all the help!

0 Kudos
ganesha31
Enthusiast
Enthusiast
Jump to solution

@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!

kiranganap100
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}}

        }

    }

}


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

0 Kudos
kiranganap100
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

0 Kudos
hhassan0513
Contributor
Contributor
Jump to solution

Hi Luc,

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

Thanks,

Hicham

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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'}

}


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

0 Kudos
hhassan0513
Contributor
Contributor
Jump to solution

Excellent!

it worked and it is very fast too.

Thanks, Luc!

0 Kudos