Author  Posts 
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? 

Just a guess, but, in some of your tests, it's expecting a string, so it's converting for you. $x[0].tostring() 

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

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? 

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

Assuming there is a unique identifier in the properties: (($ywhereobject {$_.name eq $x[0].Name}) ne $null) Or, if you really want to compare the entire object. compare $x[0] $y IncludeEqual ExcludeDifferentselect expand inputobject 

As folks have eluded, you need to compare apples to apples, or in this instance string to string or object to object. CompareObject 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") "CompareObject: {0}" f ((CompareObject $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 CompareObject: True 

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.