PowerCLI

 View Only
Expand all | Collapse all

get-view filter

  • 1.  get-view filter

    Posted Sep 06, 2017 10:54 PM

    any idea how I can filter for VMs with powered on state and guest OS of windows using get-view?

    get-view -viewtype virtualmachine -property Name,summary.runtime -filter @{ $_.summary.runtime.powerstate -eq "PoweredOn"} is not working



  • 2.  RE: get-view filter

    Posted Sep 06, 2017 11:09 PM

    If you don't have the VMware Tools installed, you can do

    Get-View -ViewType VirtualMachine -Filter @{

            'Runtime.PowerState'='poweredOn'

            'Config.GuestFullName'='Window'

          } |

    Select Name

    For a more accurate filter with VMware Tools, you can be more specific on the Windows OS you want to filter.

    Like for example

    Get-View -ViewType VirtualMachine -Filter @{

            'Runtime.PowerState'='poweredOn'

            'Guest.GuestFullName'='Windows Server 2016'

          } |

    Select Name



  • 3.  RE: get-view filter

    Posted Sep 07, 2017 01:02 PM

    thanks Luc. when and what is the -property for ?



  • 4.  RE: get-view filter
    Best Answer

    Posted Sep 07, 2017 01:20 PM

    You mean the -Property parameter on the Get-View cmdlet?

    Normally the Get-View cmdlets returns the object(s) with values for all properties of each object.

    If you only need a specific property, you can specify it on this parameter.

    The fewer properties the faster the Get-View cmdlet will be, especially in bigger environments.

    For example the following will return all VirtualMachine objects, but only the Name property will have a value.

    Get-View -ViewType VirtualMachine -Property Name



  • 5.  RE: get-view filter

    Posted Sep 07, 2017 03:33 PM

    Thanks



  • 6.  RE: get-view filter

    Posted Sep 08, 2017 09:05 PM

    Hi

    is there a faster way to run this query?



  • 7.  RE: get-view filter

    Posted Sep 08, 2017 09:43 PM

    Try like this

    $filterHash = @{

        'Runtime.PowerState'="^(?!poweredOff)"

        'Guest.GuestFamily'="^(?!windowsGuest)"

        'Guest.ToolsStatus'="toolsOld"  

    }

    $properties = 'Name','Runtime.PowerState',

        'Guest.GuestFamily','Guest.ToolsStatus','Guest.GuestFullName'

    Get-View -ViewType VirtualMachine -Property $properties -Filter $filterHash |

    Select Name,

        @{N='OS';E={$_.Guest.GuestFullName}},

        @{N='ToolsStatus';E={$_.Guest.ToolsStatus}} |

    Export-Csv vmtools-linux.csv -NoTypeInformation -UseCulture



  • 8.  RE: get-view filter

    Posted Sep 13, 2017 01:52 PM

    Thanks Luc.

    another question on filter

    how do I filter on name vmhosts that match

    get-view -viewtype hostsystem -filter @{'Name'="vmhost006*"} -property NAme | select Name

    I want to filter on hosts that only match vmhost006*



  • 9.  RE: get-view filter

    Posted Sep 13, 2017 02:11 PM

    Use the RegEx anchor '^'

    Get-View -viewtype HostSystem -Filter @{'Name'="^vmhost006"} -Property NAme | select Name



  • 10.  RE: get-view filter

    Posted Dec 21, 2020 01:18 AM

    Trying to get vms with portgroup info but not working

     

    'Guest.Net.NEtwork'="*"

     

     

    but not working

     

    any idea?

     



  • 11.  RE: get-view filter

    Posted Dec 21, 2020 02:10 AM

    actually trying to do this with get-view

     

    get-vm | ? {$_.powerstate -eq "PoweredON" -and $_.extensiondata.guest.toolsstatus -ne "Toolsok"} | select Name, @{n="Tools Status"; E={$_.extensiondata.guest.Toolsstatus}}, @{n="Guest OS";e={$_.extensiondata.guest.guestfullname}}, @{n="PortGroup";e={$_.extensiondata.guest.net.network}}

    , @{n="vcenter";e={$_.Uid.Split(":")[0].Split("@")[1]}} | out-gridview

     

    any idea?



  • 12.  RE: get-view filter
    Best Answer

    Posted Dec 21, 2020 06:23 AM

    Try something like this

    Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn';'Guest.ToolsState'='ToolsOk'} |
    Select Name,
        @{N="Tools Status"; E={$_.Guest.Toolsstatus}}, 
        @{N="Guest OS";E={$_.Guest.Guestfullname}}, 
        @{N="PortGroup";E={$_.Guest.Net.Network -join '|'}},
        @{N="vcenter";E={([uri]$_.Client.ServiceUrl).Host}} 


  • 13.  RE: get-view filter

    Posted Dec 07, 2023 02:47 PM

    The performance varies for me, in many occasiont the get-vm and filter aftwards is faster...

    Will i see big benefits if the latency to the VCenter is really bad?

    Is the big benefit that i use less API calls with the Get-View option?

     



  • 14.  RE: get-view filter

    Posted Dec 08, 2023 10:17 AM

    The Get-VM cmdlet has been optimised, so the execution time difference between Get-VM and Get-View has been minimised.
    The Get-View cmdlet still has an advantage when you only need to extract a limited set of properties.

    When the VCSA latency is high I suspect both cmdlets will suffer.