PowerCLI

 View Only
  • 1.  Why can I not filter an object by property when property equals "xyz"?

    Posted Jun 19, 2024 10:53 PM

    I cannot get the following code to only return results based on the host name:

    PS>$ESXiHost = get-vmhost 'MyHost15.MyDomain.Com'
    PS>$esxihost.name
    MyHost15.MyDomain.Com
    
    PS>$oSwitchObject =  Get-VirtualSwitch -Name 'MyDVSwitch' -Distributed
    PS> $oSwitchObject
    
    Name                           NumPorts   Mtu   Notes
    ----                           --------   ---   -----
    MyDVSwitch               782        9000
    

    I have checked that the host is attached to the mentioned dvSwitch using the vCenter GUI.

    Now to return the VDPort information for only this host:

    Get-VDPort -VDSwitch $oSwitchObject.Name | where-object { ($_.proxyhost -eq $esxihost.name) } | Select *
    Get-VDPort -VDSwitch $oSwitchObject.Name | where-object { ($_.proxyhost -eq 'MyHost15.MyDomain.Com') } | Select *
    
    

    HOWEVER, the following commands DO work:

    Get-VDPort -VDSwitch $oSwitchObject.Name | where-object { ($_.proxyhost -like $esxihost.name) } | Select *
    Get-VDPort -VDSwitch $oSwitchObject.Name | where-object { ($_.proxyhost -like 'MyHost15.MyDomain.Com') } | Select *

    Why does "-Like" work and not "-eq"?

    This is the output of the working commands:

    ExtensionData      : VMware.Vim.DistributedVirtualPort
    Key                : 9
    Description        :
    IsBlocked          : False
    IsBlockedInherited : True
    IsLinkUp           : False
    MacAddress         :
    VlanConfiguration  : VLAN 1234
    ConnectedEntity    : Network adapter 1
    ProxyHost          : MyHost15.MyDomain.Com
    Portgroup          : vLAN1234
    Switch             : MySwitch
    Name               :
    Id                 : 9
    Uid                : /VIServer=MyDomain\jmilano@MyvCenter:443/DistributedPort=9/



  • 2.  RE: Why can I not filter an object by property when property equals "xyz"?

    Posted Jun 20, 2024 03:20 AM

    Because the ProxyHost property is not a single String but a VMware.VimAutomation.Vds.Types.V1.VDPort object.
    To use the -eq operand, use the Name property of the ProxyHost object.

    Get-VDPort -VDSwitch $oSwitchObject.Name | where-object { ($_.proxyhost.Name -eq $esxihost.name) } | Select *

     



    ------------------------------


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


    ------------------------------



  • 3.  RE: Why can I not filter an object by property when property equals "xyz"?

    Posted Jun 20, 2024 04:51 AM

    Thanks for that. I was running the command from the command line and got this:

    PS > Get-VDPort -VDSwitch $oSwitchObject.Name | Select *
    
    ExtensionData      : VMware.Vim.DistributedVirtualPort
    Key                : 5
    Description        : 
    IsBlocked          : False
    IsBlockedInherited : True
    IsLinkUp           : True
    MacAddress         : 00:50:56:a0:fb:76
    VlanConfiguration  : VLAN 100
    ConnectedEntity    : Network adapter 1
    ProxyHost          : MyHost15.MyDomain.Com
    Portgroup          : Management100
    Switch             : dvSwitch-MySwitch
    Name               : 
    Id                 : 5
    Uid                : /VIServer=MyDomain\jmilano@MyvCenter:443/DistributedPort=5/

    From the result, it seemed that ProxyHost was a text property not an object.

    I ran the code in my Powershell ISE and it does come back as an object.

    Thank you.




  • 4.  RE: Why can I not filter an object by property when property equals "xyz"?

    Posted Jun 21, 2024 02:24 PM

    Console output is most of the time not the best way to try and determine what is actually returned.
    PS uses these formatting files (.PS1XML) to determine what is shown in the console for an object.

    Always the GetType() method to determine an object's type.



    ------------------------------


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


    ------------------------------



  • 5.  RE: Why can I not filter an object by property when property equals "xyz"?

    Posted Jun 22, 2024 07:34 AM

    As well as using GetType(), I often pipe an object through Convertto-json (sometimes having to add -depth) to visualise an object. Pipe that into scb (alias for Set-Clipboard) and paste into notepad for an easier experience than in a console window.