VMware Cloud Community
harkamal
Expert
Expert
Jump to solution

using Get-VIew -filter

How can i search a host using its MoRef, below code does not work:

Get-View -viewtype HostSystem -Filter @ {"HostId"="Host-139"}

0 Kudos
1 Solution

Accepted Solutions
avlieshout
VMware Employee
VMware Employee
Jump to solution

Luc,

I was referring to the construction of the ManagedObjectReferen object.

I wonder why you can't use the MoRef property. Maybe it's a bug.

I can use other moref properties, but NOT the MoRef property

The following example works:

$v = get-view -ViewType virtualmachine -filter @{"runtime.host"="^host-2116$"}

This wil return all vms from host-2116. The runtime.host porperty is of type ManagedObjectReference.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
21 Replies
LucD
Leadership
Leadership
Jump to solution

The 'key' part has to be a property of the HostSystem object, so you would have to look for a property under Extensiondata.

And HostId doesn't exist there.

You can use nested properties for the Key part. So "MoRef.Value" is not accepted either.

Afaik the -Filter parameter only accepts hashtable{string,string} arguments so you can't pass a MoRef in the Value property either.

But why can't you do this with the info you have

$t = New-Object VMware.Vim.ManagedObjectReference

$t.Type = "HostSystem"

$t.Value = "host-139"

Get-View -Id $t


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

0 Kudos
harkamal
Expert
Expert
Jump to solution

I need to search for 5-7 hosts with MoRef as only identifier to search for.

So I search for viewType as hostSystem and want to filter output for few hosts only and with selective properties only. What can be done here ?

0 Kudos
harkamal
Expert
Expert
Jump to solution

How to enumerate "extensionData" or property names that we can search for ?

tried Moref.Value, HostId.Value --doesn't work

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do the filtering outside of the Get-View cmdlet

Something like this for example

$targets = "host-123","host-456","host-789"
$servers = Get-View -ViewType HostSystem | where {$targets -contains $_.Extensiondata.MoRef.Value}


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

avlieshout
VMware Employee
VMware Employee
Jump to solution

Remember that the Value property of the MoRef is Case Sensitive!

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the Key part of the -Flter parameter only accepts level 1 properties.

And it doesn't seem to convert complex properties to [string] format either

Get-View -ViewType HostSystem -Filter @{"MoRef"="HostSystem-host-123"}

doesn't work either. It throws an InvalidProperty error.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, but the -contains operator isn't case-sensitive

Not sure I get what you are trying to say ?


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

0 Kudos
harkamal
Expert
Expert
Jump to solution

i want to retrieve only selective hosts, so if i do the filter outside of get-view, it will take lot of time to first query vcenter and then i filter at client side.

i wish to filter the object at server level, so i get what i want, just what i want

0 Kudos
harkamal
Expert
Expert
Jump to solution

can tou give us an example for moref as filter

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you will have to code your own PropertyCollector and use a PropertyFilter I'm afraid.

The -Filter implementation on the Get-View doesn't seem to allow what you are aiming to do.


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

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Luc,

I was referring to the construction of the ManagedObjectReferen object.

I wonder why you can't use the MoRef property. Maybe it's a bug.

I can use other moref properties, but NOT the MoRef property

The following example works:

$v = get-view -ViewType virtualmachine -filter @{"runtime.host"="^host-2116$"}

This wil return all vms from host-2116. The runtime.host porperty is of type ManagedObjectReference.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
harkamal
Expert
Expert
Jump to solution

Thanks for the example. MoRef filter appears to be buggy.

I found out an equivalent property for hostSystem and observed the propertyname or value is case in-insensitive, it works.

get-view -viewtype hostsystem -filter @{"CoNfig.hoST"="hOsT-107"}

thanks a lot, much appreciated.

0 Kudos
harkamal
Expert
Expert
Jump to solution

By the way, how do you get "ExtensionData", it's not there for a host view, atleast for me 🙂

Thanks for being such a great help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, it exists on the .Net object not on the HostSystem object.

I wanted to say that you should look at the properties under there

Get-VMHost MyHost | Select -ExpandProperty Extensiondata

The Extensiondata property maps to the HostSystem object.


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

0 Kudos
harkamal
Expert
Expert
Jump to solution

😞 not working for me PS M:\Documents\MyData\csv_scripts> get-vmhost 50001.domain.net  | fl * State                : Connected ConnectionState      : Connected PowerState            : PoweredOn VMSwapfileDatastoreId : VMSwapfilePolicy      : Inherit ParentId              : ClusterComputeResource-domain-c3204 Manufacturer          : HP Model                : ProLiant BL685c G1 NumCpu                : 8 CpuTotalMhz          : 19288 CpuUsageMhz          : 1975 MemoryTotalMB        : 32766 MemoryUsageMB        : 11733 ProcessorType        : Dual-Core AMD Opteron(tm) Processor 8216 HyperthreadingActive  : False TimeZone              : Europe/Zurich Version              : 3.5.0 Build                : 163429 CustomFields          : {} Id                    : HostSystem-host-107 Name                  :  50001.domain.net PS M:\Documents\MyData\csv_scripts> get-vmhost  50001.domain.net | select -ExpandProperty ExtensionData Select-Object : Property "ExtensionData" cannot be found. At line:1 char:46 + get-vmhost  50001.domain.net  | select <<<<  -ExpandProperty ExtensionData     + CategoryInfo          : InvalidArgument: ( 50001.domain.net :PSObject) [Select-Object]   , PSArgumentException     + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCom   mand

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are probably using a pre-PowerCLI 4.1 build.


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

0 Kudos
yboychev
Hot Shot
Hot Shot
Jump to solution

Hi all,

Just to bring some clarification about using the filter parameter of the Get-View cmdlet. Inside the hash table you should provide name of properties that correspond to the actual manage object properties on the server. That include nested properties, not only first level ones. You can find out the properties of the objects in the API reference guide (yes, this is kind of advanced feature so it does require as Luc say "that extra step"). Some examples:

Get-View -ViewType HostSystem -Filter @{"name" = "MyHost"}

Get-View -ViewType HostSystem -Filter @{"runtime.powerState" = "poweredOn"} NB property names are case sensitive Smiley Sad

Yavor,

PowerCLI team

Using filter parameter for Get-View is performance optimized but actually filtering is not done completely server side but these are implementation details so I will not bother you with them now.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks for the info Yavor.

And yes, you are right, the filter accepts nested properties, contrary to what I first thought.

But do you agree that filtering on the MoRef with ViewType HostSystem seems to be indeed a bug ?

Seen that it does work for ViewType VirtualMachine and other managed objects.


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

0 Kudos
yboychev
Hot Shot
Hot Shot
Jump to solution

Luc,

I'm not sure if I get your question correct. If you are pointing to the case:

Get-View -ViewType HostSystem -Filter @{"MoRef"="HostSystem-host-123"}

I don't think this is a bug, since "MoRef" is not a property of the managed object HostSystem. Actually it is not a property, I should clarify a name of a property of any managed object on the server. About seeing it working for virtual machines: that is the part that brings doubts in me whether I understand you question correctly. If we change HostSystem with VirtualMachine in the above example, I don't see how that will work.

Yavor,

PowerCLI team

0 Kudos