VMware Cloud Community
sanshis
Enthusiast
Enthusiast

get-view -filter with multiple properties

Hi All;

Recently an interesting case came in front of me, and i am not able to find any simple solution for it.. Smiley Sad

Ok; heres the case

I have a VMname but i don't know whether its a vCenter Name of that VM or the DNS name i.e. Guest.HostName. so if i have to get this VM using get-view i have to run same command with different properties;

my question is - Is there any way i can combine these two properties??? like we do and operation as follows

$Vmname= Get-View -ViewType VirtualMachine -Property Name -Filter @{“Name”=”^MyVmName$”; “Guest.hostName”=”^MyVmName$”}

Above line will return vm object if both the conditions satisfy; but i want any one property to filter. with get-view.

$Vmname= Get-View -ViewType VirtualMachine -Property Name -Filter @{“Name”=”^MyVmName$” ??? “Guest.hostName”=”^MyVmName$"} ???
is there any way to replace those question-marks in Filter array and get desire output..

Regards
Sanshis

0 Kudos
3 Replies
Grzesiekk
Expert
Expert

-Filter <Hashtable>

     Specifies a hash of <name>-<value> pairs, where <name> represents the property value to test, and <value> represents a regex pattern the

     property must match. If more than one pair is present, all the patterns must match.

get-view -ViewType virtualmachine -Property name,guest.hostname |?{$_.Name -eq 'name1' -or $_.Guest.Hostname -eq 'sth'}

--- @blog https://grzegorzkulikowski.info
0 Kudos
sanshis
Enthusiast
Enthusiast

Thx for your reply.

but this will be a very slow process and environment having 12K VMs this is not useful Smiley Sad. i was looking for something quick. currently i am doing with if else ladder and it seem that's the best way.

Regards
Sanshis

0 Kudos
Grzesiekk
Expert
Expert

Have you measured the time it takes to run: get-view -viewtype virtualmachine -property name,guest.hostname ? Even with 12k vms , that should not take longer than i guess 30 seconds,

--- @blog https://grzegorzkulikowski.info
0 Kudos