When Equals doesn't

Not sure what I am missing here but I just don’t see why these two aren’t equal.

Input

$POM = "@{PrefixOrigin=Manual}"
$SIP = Get-NetIPAddress -InterfaceAlias Wi-Fi -AddressFamily IPv4 | Select-Object PrefixOrigin
write-host $POM
write-host $SIP
If ($SIP -eq $POM) {
 write-host "IP address is Static " -foreground Blue -BackgroundColor Green
 }Else{
 write-host "IP address is Not Static" -foreground Yellow -BackgroundColor DarkMagenta
 }

output
@{PrefixOrigin=Manual}
@{PrefixOrigin=Manual}
IP address is Not Static

David,
Welcome to the forum. :wave:t4:

How about this?

$SIP = Get-NetIPAddress -InterfaceAlias Wi-Fi -AddressFamily IPv4
If ($SIP.PrefixOrigin -eq 'Manual') {
    write-host "IP address is Static " -foreground Blue -BackgroundColor Green
}
Else {
    write-host "IP address is Not Static" -foreground Yellow -BackgroundColor DarkMagenta
}

If it’s actually about why your comparison did not work try to output this:

$POM.GetType()
$SIP.GetType()

:wink:

I think this is what you are trying to do.

$POM = @{PrefixOrigin='Manual'}
$SIP = Get-NetIPAddress -InterfaceAlias Wi-Fi -AddressFamily IPv4 | Select-Object PrefixOrigin
write-host $POM.PrefixOrigin
write-host $SIP.PrefixOrigin
If ($SIP.PrefixOrigin -eq $POM.PrefixOrigin) {
 write-host "IP address is Static " -foreground Blue -BackgroundColor Green
 }Else{
 write-host "IP address is Not Static" -foreground Yellow -BackgroundColor DarkMagenta
 }
2 Likes

Yes That is exactly what I needed but couldn’t figure out.
Thanks ferc!

You were close. The first line required a small change in the position of the quotes. Then, you just needed to explicitly refer to the PrefixOrigin value of $POM and $SIP which is what you wanted to compare.

This should also work.

$POM = 'Manual'
$SIP = Get-NetIPAddress -InterfaceAlias Wi-Fi -AddressFamily IPv4 | Select-Object PrefixOrigin
write-host $POM
write-host $SIP.PrefixOrigin
If ($SIP.PrefixOrigin -eq $POM) {
 write-host "IP address is Static " -foreground Blue -BackgroundColor Green
 }Else{
 write-host "IP address is Not Static" -foreground Yellow -BackgroundColor DarkMagenta
 }

Here , instead of assigning a key value pair to $POM, I used a string with the value that is required for comparison purposes later in the If/Else statement. Since it is no longer a key value pair, $POM will return “Manual” without the need to refer to its PrefixOrigin property (that no longer exists in this example anyway).

Just wanted to add that the reason they weren’t equal is because they are objects. Even the same exact hash tables are not equal, because they aren’t the same object. Situations like this you could easily check a property value (or multiple in other circumstances.)

See the following demo

$obj1 = @{a='b'}
$obj2 = @{a='b'}

$obj1 -eq $obj2
False

$obj1.a -eq $obj2.a
True
2 Likes