PowerCLI

 View Only
  • 1.  Using get-view, how to get guest VMs on a vmhost or in a cluster/datacenter, when the parents were also retrieved via get-view

    Posted Jul 20, 2017 09:33 PM

    Hi LucD et al.  -

    In my functions, I am trying whenever possible to substitute get-view for get-vm (and get-vmhost, get-cluster, get-datacenter, etc.), because it's soooo much faster.

    I still can't figure out the equivalent to:

    $ get-vmhost | get-vm

    For example, if I have a vmhost object retrieved via get-view, how do I pipe that (or otherwise reference it) to get all the guests on that host?

    $ Get-View -ViewType HostSystem -Filter @{Name="hostname.domain.local"}  | get-view -ViewType VirtualMachine  # this does not work, obviously

    (and yes, I'd add -Property once I figured out which properties I need )

    I'm on 6.5.0.2, if that makes any difference.

    Thanks!

    Paula



  • 2.  RE: Using get-view, how to get guest VMs on a vmhost or in a cluster/datacenter, when the parents were also retrieved via get-view
    Best Answer

    Posted Jul 20, 2017 10:00 PM

    One way is using the SearchRoot parameter.

    Like this for example

    $esxName = 'esx1'

    $esx = Get-View -ViewType HostSystem -Property Name -Filter @{'Name'=$esxName}

    $vm = Get-View -ViewType VirtualMachine -SearchRoot $esx.MoRef

    $vm | select Name

     

    But the easiest one is like this imho

    $esxName = 'esx1'

    $esx = Get-View -ViewType HostSystem -Filter @{'Name'=$esxName}

    Get-View -Id $esx.Vm

     



  • 3.  RE: Using get-view, how to get guest VMs on a vmhost or in a cluster/datacenter, when the parents were also retrieved via get-view

    Posted Jul 21, 2017 07:23 PM

    Thanks as always, Luc.

    I think the first approach will work best for me. I have to fiddle around with properties to make sure I'm filtering out as much as possible for speed.

    I like the simplicity of the 2nd approach but it seems like it might make it more expensive to look for all guests in a cluster or datacenter, right? Wouldn't I have to do something like the following?

    $ClusterObj = Get-View -ViewType ClusterComputeResource -Filter @{Name = "^$Cluster"}

    $VMObj = Get-View -id (Get-View -ID $ClusterObj.Host).VM

    :smileywink:

    pk



  • 4.  RE: Using get-view, how to get guest VMs on a vmhost or in a cluster/datacenter, when the parents were also retrieved via get-view

    Posted Jul 21, 2017 07:36 PM

    Correct, if you want to that for a cluster, you would have to first get all the ESXi member nodes, and then for each ESXi node the VMs.

    But I suspect, vSphere does something similar behind the covers.

    The difference could be in the overhead, if any, that the additional Get-View introduces.

    The only way to know, would be to time with Measure-Command.

    But then again, you would need to be running against a very big environment before seeing any significant differences in execution time I assume.