VMware Cloud Community
marc045
Contributor
Contributor
Jump to solution

Optimum code for retrieving all VMs in a ResourcePool

Hi All,

What is the optimum (fastest) code for retreiving all VMs in a Resource Pool?

I have this:

$myVMs = get-vm | where { $_.Resourcepool -eq "MYRP" }

[1]

Does this actually retreive ALL VMs first, and then filter on resource pool second (in which case it would be "slow")?

I also have this:

$myRP = get-resourcepool "MYRP"

$myRP.extensiondata.vm

This outputs the VM MoRef's.

[2]

How do I use get-view to then only pull info about those specific MoRefs and does this method have potential to be significantly more performing than the get-vm method detailed above?

get-view -viewtype VirtualMachine -property MoRef does not work

Regards,

marc0

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Hi Marco,

1) Yes, this call will first retrieve all VMs and then perform filtering on the resource pool property.

I'd suggest using the following call that will retrieve only the VMs in the desired resource pool. This will give you better performance:

$resourcePool = Get-ResourcePool "MYRP"

Get-VM -Location $resourcePool

# Or you can use this:

Get-VM -Location "MYRP"

2) You can use Get-View to retrieve the objects by MoRef like this:

$vmViews = Get-View -Id $myRP.extensiondata.vm

# If you need the VM objects rather than their views, then you'll also need to do the following:

$vms = $vmViews | Get-VIObjectByVIView

Regards,

Dimitar

View solution in original post

Reply
0 Kudos
1 Reply
admin
Immortal
Immortal
Jump to solution

Hi Marco,

1) Yes, this call will first retrieve all VMs and then perform filtering on the resource pool property.

I'd suggest using the following call that will retrieve only the VMs in the desired resource pool. This will give you better performance:

$resourcePool = Get-ResourcePool "MYRP"

Get-VM -Location $resourcePool

# Or you can use this:

Get-VM -Location "MYRP"

2) You can use Get-View to retrieve the objects by MoRef like this:

$vmViews = Get-View -Id $myRP.extensiondata.vm

# If you need the VM objects rather than their views, then you'll also need to do the following:

$vms = $vmViews | Get-VIObjectByVIView

Regards,

Dimitar

Reply
0 Kudos