Automation

 View Only
  • 1.  Add a filter on get-view to only return results from a specific cluster

    Posted Nov 06, 2024 01:58 PM
    Edited by Jason McClellan Nov 06, 2024 02:29 PM

    Hi All,
    I've had some luck using get-filter to filter on individual VMs by name, but how can I use get-view -filter to only return results from a specific cluster? 

    Get-View -ViewType VirtualMachine -Filter @{"Cluster"=MYCLUSTER"} -Property Parent, Config, Summary, Runtime |
    ForEach-Object -Process {
      $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
      [PSCustomObject] @{
        Name = $_.config.name
        GuestId = $_.Config.GuestId
        vCenter = ([uri]$_.Client.ServiceUrl).Host
        Folder = (Get-View -Id $_.Parent -Property Name).Name
        'IP Address' = $_.Summary.Guest.IpAddress
        Template = $_.summary.config.Template
        VMHost = $esx.Name
        Cluster = (Get-View -Id $esx.Parent -Property Name).Name
        Notes = $_.summary.config.annotation
      }
    }

    This is erroring out.



  • 2.  RE: Add a filter on get-view to only return results from a specific cluster
    Best Answer

    Posted Nov 06, 2024 02:00 PM

    Use the SearchRoot parameter instead.

    Get-View -ViewType VirtualMachine -SearchRoot ((Get-Cluster -Name MyCluster).ExtensionData.MoRef) -Property Parent, Config, Summary, Runtime


    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 3.  RE: Add a filter on get-view to only return results from a specific cluster

    Posted Nov 06, 2024 03:36 PM

    Thanks LucD, works great, however I'm still having an issue with my results. It appears that there are many VMs which are replicated, so I believe that may be why I'm getting results from multiple vCenters like this:

    Name: MyVM
    Cluster    : {MYCLUSTERDR, MYCLUSTER, MYCLUSTER3}

    When exported to csv the field simply says System.Object[]

    Can I and an additional qualifier to  -SearchRoot ? Something like -SearchRoot ((Get-Cluster -Name MyCluster).ExtensionData.MoRef) -and ([uri]$_.Client.ServiceUrl).MYVCENTER ?

    The goal would be to further isolate my results to one vCenter while remaining connected to all of them.




  • 4.  RE: Add a filter on get-view to only return results from a specific cluster

    Posted Nov 06, 2024 03:43 PM

    You can use the Server parameter on the Get-Cluster cmdlet

    Get-View -ViewType VirtualMachine -SearchRoot ((Get-Cluster -Name MyCluster -Server MyvCenter).ExtensionData.MoRef) -Property Parent, Config, Summary, Runtime


    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 5.  RE: Add a filter on get-view to only return results from a specific cluster

    Posted Nov 06, 2024 05:53 PM

    That worked, thank you.