Git Product home page Git Product logo

Comments (4)

scriptingstudio avatar scriptingstudio commented on June 3, 2024

With objects a good practice is using parameter -property

from powershell.

mklement0 avatar mklement0 commented on June 3, 2024

How about the following, then?

Compare-Object @{ Instance = $instance1 } @{ Instance = $instance2 } -Property Instance

Still broken.

The point is that it doesn't matter whether the problem arises in the context of whole-object or property-value comparisons.

Sure, if you know of a distinguishing property - assuming there even is one - you can use it as a workaround
(Compare-Object $instance1 $instance2 -Property Num), but not only should that not be necessary, it changes the output.

from powershell.

scriptingstudio avatar scriptingstudio commented on June 3, 2024

Broken because $instance1 and $instance2 are not scalar types. And argument @{ Instance = $instance1 } is not psobject type as help defines (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.4#inputs)

"$instance1"
Foo
"$instance2"
Foo

So Compare-Object @{ Instance = 'Foo' } @{ Instance = 'Foo' } -Property Instance has no output.

But

Compare-Object ([pscustomobject]@{ Instance = $instance1 }) ([pscustomobject]@{ Instance = 24545 }) -Property Instance
Instance SideIndicator
-------- -------------
   24545 =>
     Foo <=

works

The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method, it calls the ToString() methods of the input objects and compares the string results. You can provide one or more properties to be used for comparison. When properties are provided, the cmdlet compares the values of those properties only. The result of the comparison indicates whether a property value appeared only in the reference object (<=) or only in the difference object (=>). If the IncludeEqual parameter is used, (==) indicates the value is in both objects. If the reference or the difference objects are null ($null), Compare-Object generates a terminating error. Some examples use splatting to reduce the line length of the code samples. For more information, see about_Splatting.

from powershell.

mklement0 avatar mklement0 commented on June 3, 2024

No, it's broken for the reasons stated in the initial post.

$instance1 -eq $instance2 is (meaningfully) $false, so Compare-Object should report a difference too.

The other examples are irrelevant to this discussion, and as for the "If it can't find a suitable method" quote from the docs:

For value types (types derived from System.ValueType) there is a suitable method, by definition, and the fact that it isn't used (and therefore falls back to string comparison) constitutes the bug.

from powershell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.