PowerCLI

 View Only
Expand all | Collapse all

Is there a quicker way to get the vsphere cluster name that a VM belongs to using Get-View

  • 1.  Is there a quicker way to get the vsphere cluster name that a VM belongs to using Get-View

    Posted Apr 02, 2025 08:32 AM
    Edited by AughBT Apr 02, 2025 08:37 AM

    the following code works but its a bit slow - it is slowed down by the method I use to get the cluster.  Is there a way to get the cluster name of a VM using get-view .  Using "$_.parent" has never worked for me. So I have to use "get-cluster" which slows things down.  It takes over an hour for the script to complete with the get-cluster, without get-cluster it completes in 2 seconds.

    (connect to the vcenter(s) before running)

    & { foreach ($vCenterServer in $DefaultVIServers)
        {
          $VMHostTable= @{}
          foreach ($VMHostView in (Get-View -Server $vCenterServer -ViewType HostSystem -Property Name))
          {
            $VMHostTable["$($VMHostView.MoRef.Value)"] = $VMHostView.Name # this is required to return the host that the VM is running on.
    write-host "Processing :" $VMHostView.Name
          }
          Get-View -Server $vCenterServer -ViewType VirtualMachine -Filter @{"Config.Template"="False"} -Property Name, # get virtual machine (as opposed to esxi host)
            parent,
            Guest.HostName,
            guest.net,
            guest.toolsstatus,
            Runtime.Host,
            ##runtime.cluster,
    runtime.powerstate,
            Guest.GuestFullName,
            Config.Annotation,
            config.datastoreurl,
            Guest.GuestFullName,
          Config.Hardware.MemoryMB,
          Config.Hardware.NumCPU,
            Config.Hardware.Device|
          Select-Object -Property @{N="VM";E={$_.Name}},
          @{N="Guest OS";E={$_.Guest.GuestFullName}},
          @{N="DNS";E={$_.Guest.HostName}},
      @{N="Datastore";E={$_.config.datastoreurl.name}},
          @{N="vCenter";E={$vCenterServer.Name}},
          @{N="VMHost";E={$VMHostTable["$($_.Runtime.Host.Value)"]}}, # works
     @{N="Cluster";E={[string]$cluster=get-cluster -vm $_.name | select name;[string]$object1=$cluster.replace("@{Name=","");[string]$object2=$object1.replace("}","");$object2}} # works very slowly
        

        }
      } |
    Export-Csv -Path "VMsInfo-$(Get-Date -UFormat '%Y%m%d-%H.%M.%S').csv"  -delimiter "|" -NoTypeInformation



    ------------------------------
    NetworkAdmin9845
    ------------------------------



  • 2.  RE: Is there a quicker way to get the vsphere cluster name that a VM belongs to using Get-View

    Posted Apr 02, 2025 03:27 PM

    First, your extensive list of -Property to filter your returned VM View is likely slower than no filter, you might want to use Measure-Command to know for sure. When you are using a short list of returned properties it's almost always faster, but not so with a long list.

    Next I use a little loop like this to quickly find the cluster of a vm

    $Parent = ""; $Parent = $VMView
    IF($Parent.MoRef.Type -ne "ClusterComputeResource" -and $Parent){
            Do{$Parent = Get-View -Id $Parent.Parent -Property Name,Parent -Server $vCenter}While($Parent.MoRef.Type -ne "ClusterComputeResource" -and $Parent.Parent)
        }
        IF(($Parent.MoRef) -AND ($Parent -Is [VMware.Vim.ClusterComputeResource])){
            $VMViewCurrentClusterView = ""; $VMViewCurrentClusterView = Get-View -Id $Parent.MoRef -Server $vCenter
            $VMViewCurrentClusterView
        }