Comments (4)
With objects a good practice is using parameter -property
from powershell.
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.
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.
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)
- LICENSE.txt HOT 2
- HideTableHeaders is not persisted to Get-FormatData if defined as FormatData rather than in format file. HOT 1
- Using Invoke-WebRequest POST to upload a file is broken HOT 19
- ScriptProperty getters (-Value) can not throw errors HOT 8
- call operator & fails when used with background operator & HOT 6
- "RegisterMicrosoftUpdate.ps1" fails and freezes installer when AppLocker / WDAC rules exist (despite the script being whitelisted & running full language mode) HOT 2
- Cannot build packages from devcontainer
- Invoke-WebRequest fails with โRequest headers must contain only ASCII characters.โ for simple requests HOT 2
- [JEA] Register-PSSessionConfiguration does not "cascade" the -NoServiceRestart parameter to Set-PSSessionConfiguration when automatically calling it.
- Feature: Expose `Set-Location` history stack HOT 11
- Surrogate pairs cause misalignment in Format-Wide HOT 11
- Update behavior for getting wix 3.14 in CI.psm1 HOT 1
- is it defined/documented which newlines are used by Write-Host and Out-File HOT 8
- `Remove-Item` throws "Attempted to divide by zero" on PS 7.5.0-preview.3 HOT 6
- `using namespace` overrides previous `using namespace` uses as interactive HOT 8
- [Windows] No error when using a non-executable file in script line HOT 4
- `Remove-Item` reports incorrect number of removed items in the progress bar HOT 1
- Bug - PowerShell displays an error when loading in Windows Terminal HOT 5
- Show payload being sent when using verbose with Invoke-RestMethod and Invoke-WebRequest HOT 2
- Encode spaces with %20 instead of + when using Invoke-RestMethod and Invoke-WebRequest. HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.