Author Posts

February 21, 2017 at 7:20 pm

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?

February 21, 2017 at 7:47 pm

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

February 22, 2017 at 8:44 am

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?

February 22, 2017 at 10:37 am

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

February 22, 2017 at 1:50 pm

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

February 22, 2017 at 2:21 pm

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

February 22, 2017 at 5:50 pm

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