-contains operator

This topic contains 7 replies, has 6 voices, and was last updated by  Curtis Smith 7 months ago.

  • Author
    Posts
  • #64960

    Manuel Berfelde
    Participant

    Can someone please explain this to me?

    $x = gsv
    $y = gsv
    
    $x[0] -like $y[0]
    #returns $true, as expected
                        
    $x[0] -match $y[0]
    #returns $true, as expected
                       
    compare $x[0] $y[0] -IncludeEqual
    #returns "==", as expected    
    
    $x[0] -eq $y[0]
    #returns $false - why? It's the same service.
    
    $y -contains $x[0]
    #returns $false - same question, why?
    
  • #64965

    Ron
    Participant

    Just a guess, but, in some of your tests, it's expecting a string, so it's converting for you.

    $x[0].tostring()
    System.ServiceProcess.ServiceController

  • #64971

    Don Jones
    Keymaster

    -contains isn't a wildcard operator. See https://www.youtube.com/watch?v=m20AVEXtEkc.

    • #65005

      Manuel Berfelde
      Participant

      Thanks for the quick replies!

      So, what would be the best way to check if $y contains $x[0], or an object which is identical in it's properties and their values?

  • #65025

    Mark Prior
    Participant

    always wondered about this but managed to get what i want with -like or -eq so not been to concerned, good to know though

  • #65029

    Ron
    Participant

    Assuming there is a unique identifier in the properties:

    (($y|where-object {$_.name -eq $x[0].Name}) -ne $null)

    Or, if you really want to compare the entire object.

    compare $x[0] $y -IncludeEqual -ExcludeDifferent|select -expand inputobject
  • #65043

    Rob Simmers
    Participant

    As folks have eluded, you need to compare apples to apples, or in this instance string to string or object to object. Compare-Object will compare objects and identify if the object contains the same values. If everything matches, it would return null, which could give you a boolean comparison:

    $x = "Red"
    $y = "Red"
    
    "Compare string: {0}" -f ($x -eq $y)
    "Compare string by index: {0}" -f ($x[0] -eq $y[0])
    "Compare by like: {0}" -f ($x -like $y)
    
    $x = "Red", "Blue", "Yellow"
    $y = "Yellow", "Red", "Blue"
    
    "Compare string: {0}" -f ($x -eq $y)
    "Compare string by index: {0}" -f ($x[0] -eq $y[0])
    "Compare by like: {0}" -f ($x -like $y)
    "Value is in array: {0}" -f ($x -contains "Blue")
    "Compare-Object: {0}" -f ((Compare-Object $x $y) -eq $null)
    

    Output:

    Compare string: True
    Compare string by index: True
    Compare by like: True
    Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
    At line:11 char:1
    + "Compare string: {0}" -f ($x -eq $y)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Compare string: {0}:String) [], RuntimeException
        + FullyQualifiedErrorId : FormatError
     
    Compare string by index: False
    Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
    At line:13 char:1
    + "Compare by like: {0}" -f ($x -like $y)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Compare by like: {0}:String) [], RuntimeException
        + FullyQualifiedErrorId : FormatError
     
    Value is in array: True
    Compare-Object: True
    
  • #65046

    Curtis Smith
    Participant

    For -contains, it is quite simply because they are not the same object. Since you are running gsv two different times, it is generating two distinctly different objects that happen to have all of the same data. If you use -contains and the exact same object is actually in the variable, it does return true as expected.

    $x = gsv
    $y = gsv
    
    $y -contains $x[0]
    $x -contains $x[0]
    
    False
    True
    $x = gsv
    $y = $x
    
    $y -contains $x[0]
    $x -contains $x[0]
    
    True
    True
    $x = gsv
    $y = gsv
    $y[0] = $x[0]
    
    $y -contains $x[0]
    $x -contains $x[0]
    
    True
    True

You must be logged in to reply to this topic.