Automation

 View Only
Expand all | Collapse all

using Get-VIew -filter

  • 1.  using Get-VIew -filter

    Posted Jan 12, 2011 12:48 PM

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

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



  • 2.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:02 PM

    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



  • 3.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:12 PM

    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 ?



  • 4.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:16 PM

    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}


  • 5.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:24 PM

    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



  • 6.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:31 PM

    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.



  • 7.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:49 PM

    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



  • 8.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:51 PM

    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.



  • 9.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 02:01 PM

    :-( 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



  • 10.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 02:57 PM

    You are probably using a pre-PowerCLI 4.1 build.



  • 11.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:16 PM

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

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



  • 12.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:19 PM

    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.



  • 13.  RE: using Get-VIew -filter

    Posted Jan 13, 2011 08:36 AM

    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 :smileysad:

    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.



  • 14.  RE: using Get-VIew -filter

    Posted Jan 13, 2011 08:40 AM

    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.



  • 15.  RE: using Get-VIew -filter

    Posted Jan 13, 2011 09:50 AM

    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



  • 16.  RE: using Get-VIew -filter

    Posted Jan 13, 2011 09:56 AM

    Ok, I see waht you mean and I agree that this is not a bug but my bad interpretation of the -Filter parameter.



  • 17.  RE: using Get-VIew -filter

    Posted Jan 13, 2011 02:33 PM

    Thanks Yavor,

    Is it possible to get sub view in 1 query i.e

    $host = get-view -viewtype hostsystem -property runtime, configmanager.storagesystem

    $ds = get-view $host[0].configmanager.storagesystem

    can i get storagesystem view in 1 shot ?



  • 18.  RE: using Get-VIew -filter

    Broadcom Employee
    Posted Jan 12, 2011 01:16 PM

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



  • 19.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:23 PM

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

    Not sure I get what you are trying to say ?



  • 20.  RE: using Get-VIew -filter
    Best Answer

    Broadcom Employee
    Posted Jan 12, 2011 01:34 PM

    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.



  • 21.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:44 PM

    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.



  • 22.  RE: using Get-VIew -filter

    Posted Jan 12, 2011 01:31 PM

    can tou give us an example for moref as filter