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.
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
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
Please try by replacing in the first line
Get-View -ViewType VirtualMachine
by
Get-View -ViewType VirtualMachine -SearchRoot ((get-cluster 'YOURCLUSTERNAME').extensiondata.moref)
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.
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.
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.
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
Hope this answers your question.
Thanks sajal1, I'll check out both references.
Appreciate you taking the time to expand on this
Pete.