Whew! Now that I actually found these forums again thanks to VMware's constant need to make navigation of the VMware communities more difficult, allow me to ask my question.
Example: You have a VM with a single NIC that is connected.
If I run this command:
Get-VM <vm name> | Get-NetworkAdapter | Where-Object {$_.ConnectionState.Connected -eq "False"}
It still returns my one and only connected NIC (more details in this next example):
Get-VM <vm name> | Get-NetworkAdapter | Where-Object {$_.ConnectionState.Connected -eq "False"} | ft Name,Type,@{N="Connected";E={$_.ConnectionState.Connected}} -autosize
However, if I use:
Get-VM <vm name> | Get-NetworkAdapter | Where-Object {$_.ConnectionState.Connected -eq $false}
then it works just fine. Why does it do this when
"false" -eq $false
?
The reason for what your seeing is called silent conversion or type casting.
The Connected property has a Boolean value ($true or $false).
When you compare this with "true" (a string), the -eq operator will convert one of the operands to have the same type as the other operand.
That's why
"true" -eq $true
will return True.
Interesting to know is that PowerShell evaluates the -eq operator left-to-right.
The left operand is the example above is a string, so PS will convert the right operand ($true) to a string.
And ultimately compare the 2 strings.
Try this
"true" -ceq $true
It will return $false because the conversion of $true to a string gives "True" and the -ceq operator is a case-sensitive comparison.
Hence the $false return.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh, running PowerShell 2.0 and PowerCLI 4.1 if it matters...
The reason for what your seeing is called silent conversion or type casting.
The Connected property has a Boolean value ($true or $false).
When you compare this with "true" (a string), the -eq operator will convert one of the operands to have the same type as the other operand.
That's why
"true" -eq $true
will return True.
Interesting to know is that PowerShell evaluates the -eq operator left-to-right.
The left operand is the example above is a string, so PS will convert the right operand ($true) to a string.
And ultimately compare the 2 strings.
Try this
"true" -ceq $true
It will return $false because the conversion of $true to a string gives "True" and the -ceq operator is a case-sensitive comparison.
Hence the $false return.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah, understood now. Thanks for the intel.
Still somewhat odd that $false -ceq "False" isn't true (that converting the string "False" doesn't result in boolean $false), but apparently any non-boolean value that is not integer 0 would be typecast to boolean $true, except for $null which is neither $true nor $false according to some testing me and a buddy just did.
That is correct. You can play with explicit casting and see some, at first sight, strange results
[Boolean]"false" # Will return $true [Boolean]"" # Will return $false
Are you sure about the $null ?
[Boolean]$null # Returns $false
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Well, I guess I'm not sure. But it seems to be that way when using comparison operators...
C:\> $false -ceq $null False C:\> $false -eq $null False C:\> $true -ceq $null False C:\> $true -eq $null False
It seems to not be typecasting $null when comparing but if I force it to do so as you did...
C:\> $false -ceq [Boolean]$null True
$null is a special value of course but I didn't know the -eq behaved like that when it sees a $null.
Good to know
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference