VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

get-view filter

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

0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}} 


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

View solution in original post

13 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Thanks

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Hi

is there a faster way to run this query?

Screen Shot 2017-09-08 at 2.04.26 PM.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

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*

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Use the RegEx anchor '^'

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


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Trying to get vms with portgroup info but not working

 

'Guest.Net.NEtwork'="*"

 

 

but not working

 

any idea?

 

0 Kudos
tdubb123
Expert
Expert
Jump to solution

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?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}} 


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

RogerBad
Contributor
Contributor
Jump to solution

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?

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Tags (1)
0 Kudos