locked
$a is equal $b - but ($a -eq $b) is false !!!! RRS feed

  • Question

  • Something about byValue or byReference??? or What is it a'm missing. What is happening - and why?

    $a="Test"
    $b="Test"
    $a -eq $b (True

    $a= get-eventlog system -newest 1
    $b= get-eventlog system -newest 1

    $a -eq $b (False!!!!!!)

    I found this because i'm reading eventlogs for 20 servers each minute and streaming Error and Warning to write-host in a loop. An due to speed I have to use get-eventlog whithout "-after" and "-before"  So I have to check, if I have seen an error/warning last minute. get-eventlog -newest is very fast  


    mvh John

    Friday, April 5, 2019 5:14 PM

Answers

  • To know if 2 objects are equal, you have to compare the parameters (or use the compare-object command like mentioned above).  If you just use -eq with two objects you are comparing the memory addresses of both objects, and they will always be different.  The only way a object will equal another object is if they both exist at the same memory location, which would just make the 2 variables an alias for the same object.

    • Marked as answer by DK-John Monday, April 8, 2019 6:27 PM
    Friday, April 5, 2019 6:50 PM

All replies

  • I don't think you can compare objects that way.

    Friday, April 5, 2019 6:13 PM
  • I don't think you can compare objects that way.


    compare-object

    Would seem to be a better built-in choice.


    • Edited by Marco.Shaw Friday, April 5, 2019 6:22 PM
    Friday, April 5, 2019 6:21 PM
  • To know if 2 objects are equal, you have to compare the parameters (or use the compare-object command like mentioned above).  If you just use -eq with two objects you are comparing the memory addresses of both objects, and they will always be different.  The only way a object will equal another object is if they both exist at the same memory location, which would just make the 2 variables an alias for the same object.

    • Marked as answer by DK-John Monday, April 8, 2019 6:27 PM
    Friday, April 5, 2019 6:50 PM
  • Thanks
    I will look at again on compare-object  - but my first try do not work. 
    Compare-Object -ReferenceObject $a -DifferenceObject $b (Return nothing)

    I know I can do It - Just trying to get a better understanding.


    mvh John

    Friday, April 5, 2019 7:04 PM
  • Two records returned at different times from the event log will never compare.  Why would you ever expect them to?

    You can compare single events but what are you compare for?  Even if you return the same event in both cases they will never compare because they are two separate objects created locally by the call.  Objects will only compare equal when they are the same object unless the objects of a special comparer method that checks what is comparable.

    You can compare  objects like this

    Compare-Object $a $b -IncludeEqual | Where{$_.SideIndicator -eq '=='}


    \_(ツ)_/

    Friday, April 5, 2019 7:24 PM
  • Thanks  - apparently a string is not a real object :-)

    Wonder... 
    $a="Test1","Test2"
    $a -eq $a (Return nothing/ null)

    It is not really importen. As mrasmussen writes - I can compare the parameters.

    But I just can not stop trying to think about the reason :-)

     

    mvh John

    Friday, April 5, 2019 7:33 PM
  • Thanks  - apparently a string is not a real object :-)

    Wonder... 
    $a="Test1","Test2"
    $a -eq $a (Return nothing/ null)

    It is not really importen. As mrasmussen writes - I can compare the parameters.

    But I just can not stop trying to think about the reason :-)

     

    mvh John

    This is not q string.  It is an array.  An array is an object collection.

    PS C:\> $a="Test1","Test2"
    PS C:\> $a.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    
    
    PS C:\>


    \_(ツ)_/

    Friday, April 5, 2019 7:46 PM
  • Thanks 

    We will have a better solution for eventlog monitoring later, but until then I will get the -newest  100 event from each server/eventlog each minut. That is the reason I see the same event.
    I have a filter working, after using parameters for timewritten, machinename, entrytype, message.   

    And thanks for the tip about how to use compare-object.


    mvh John

    Friday, April 5, 2019 7:54 PM
  • To compare two log items for equality use the events index.

     $a.RecordId -eq $b.RecordId

    If the id is identical then the copies of the record are from the same record.

    Get-Winevent -Logname System -Max 100 | Where{ $_.RecordId -notin $oldrecords.RecordId }

    This filters out all records from the previous query.


    \_(ツ)_/

    Friday, April 5, 2019 8:27 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, April 8, 2019 7:50 AM
  • To know if 2 objects are equal, you have to compare the parameters (or use the compare-object command like mentioned above).  If you just use -eq with two objects you are comparing the memory addresses of both objects, and they will always be different.  The only way a object will equal another object is if they both exist at the same memory location, which would just make the 2 variables an alias for the same object.

    Or if the objects support "ICompare" interface.


    \_(ツ)_/

    Monday, April 8, 2019 6:31 PM