VMware Cloud Community
PeterLeeman
Contributor
Contributor

Get virtual machine details from specified cluster using Get-View

Hi All

I'm trying to collect virtual machine details such as powerstate and memory usage within a specific cluster.  I have the following code which collects those details but it gets the details of both clusters the Vcenter is managing.

Get-View -ViewType VirtualMachine -Property Name, Runtime.PowerState,Config.Hardware.MemoryMB,Summary.QuickStats.GuestMemoryUsage,Summary.QuickStats.HostMemoryUsage| %{

# return a new object with info about this VirtualMachine

$obj = New-Object -Type PSObject -Property ([ordered]@{

Name = $_.Name

PowerState = $_.Runtime.PowerState

MemoryMB = $_.Config.Hardware.MemoryMB

HostMemoryUsage = $_.Summary.QuickStats.HostMemoryUsage

VmMemoryUsedMB = $_.Summary.QuickStats.GuestMemoryUsage

}) ## end new-object

$outputArray += $obj # Add the object to the array

} ## end foreach-object

I tried modifying the first line to specify the SearchRoot:

Get-View-ViewTypeVirtualMachine-Property Name, Config-SearchRoot Cluster001,

Runtime.PowerState,Config.Hardware.MemoryMB,Summary.QuickStats.GuestMemoryUsage,Summary.QuickStats.HostMemoryUsage| %{

But I get the following error:

Get-View : Cannot convert 'System.Object[]' to the type 'VMware.Vim.ManagedObjectReference' required by

parameter 'SearchRoot'. Specified method is not supported.

Any help greatly appreciated.

Pete.

7 Replies
sajal1
Hot Shot
Hot Shot

What about the following code?

$vms = Get-Cluster 'Cluster001' | Get-VM

$outputArray = @()

Foreach ($vm in $vms) {

     $obj = New-Object -Type PSObject -Property ([ordered]@{ 

     Name = $vm.Name 

     PowerState = $vm.ExtensionData.Runtime.PowerState 

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

     HostMemoryUsage = $vm.ExtensionData.Summary.QuickStats.HostMemoryUsage 

     VmMemoryUsedMB = $vm.ExtensionData.Summary.QuickStats.GuestMemoryUsage 

     }) ## end new-object 

     $outputArray += $obj # Add the object to the array 

     } ## end foreach-object

$outputArray | FT

Reply
0 Kudos
sajal1
Hot Shot
Hot Shot

Otherwise go for the following format

Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster 'Cluster001').id -Property Name, Runtime.PowerState,Config.Hardware.MemoryMB,Summary.QuickStats.GuestMemoryUsage,Summary.QuickStats.HostMemoryUsage | FT

ccalvetTCC
Enthusiast
Enthusiast

Please try by replacing in the first line

Get-View -ViewType VirtualMachine

by

Get-View -ViewType VirtualMachine -SearchRoot ((get-cluster 'YOURCLUSTERNAME').extensiondata.moref)

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
PeterLeeman
Contributor
Contributor

Thanks for the response.  I was hoping to keep with the get-view as looking around on the web get-view appears to be quicker.  Tried your second option and it worked a treat.  Thanks again.

Reply
0 Kudos
PeterLeeman
Contributor
Contributor

Thanks for the response.  Both saja1 and your option worked a treat.  Would you mind telling me what the difference is between adding '.extensiondata.moref' and '.id'.  Just wondering if I should use one instead of the other in future.

Thanks.

Pete.

Reply
0 Kudos
sajal1
Hot Shot
Hot Shot

Hi,

MoRef ID is the managed object Reference ID. There is a nice blogpost from Danilo here, How to (quickly) match a moRef ID to a name in VMware vSphere | Danilo Chiavari. ‌Please go through it for more detail. For your reference the difference between the two is, when you access MoRef, you are accessing the referenced object but accessing the ID gives the Id of the Object itself and hence is a String. For this example it will not make much difference. But the actual difference will be when you access the API's and try to do something or build an object. Because the Id will refer to the object itself but the MoRef may be same as the ID or different as the object may in turn reference another object.

Image 003.png

For details about this you can check Chapter 9 (Managing the vSphere API) of the book Mastering PowerCLI (http://bit.ly/1Nafgjh). Though it does not make much difference in your example but to be on the safer side I will work with the ID Smiley Happy

Hope this answers your question.

PeterLeeman
Contributor
Contributor

Thanks sajal1, I'll check out both references.

Appreciate you taking the time to expand on this Smiley Happy

Pete.

Reply
0 Kudos