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)
- Mount-DiskImage does not assign driver letter every third time HOT 2
- Does PowerShell support the real "Stream" response? HOT 5
- v7.4.2 not in Ubuntu 22.04 Linux package repository for Microsoft products HOT 3
- `Finally` block isn't executed upon Ctrl+C HOT 36
- Make `ConvertFrom-StringData` more error tolerant HOT 9
- Add-Type ReferencedAssemblies only works with full file paths now? HOT 1
- .Net 8 in PowerShell 7 System.Private.CoreLib fails to register. HOT 41
- `ConvertFrom-StringData` mistakenly emits a statement-terminating error when one of its input strings isn't well-formed HOT 2
- Redirection operator `>` should not be hardcoded to files HOT 6
- Update from System.Data.SqlClient to Microsoft.Data.SqlClient HOT 3
- .NET App using Powershell.SDK fails to run, when building as SingeFile HOT 2
- Add a command which gets the System Lockdown mode information HOT 2
- Overriding .NETs ToString() method can cause unexpected behaviour when using Transcripts HOT 1
- Invoke-WebRequest needs SupportsShouldProcess
- using `Get-AppxPackage` when `SYSTEMDRIVE` env var is not set creates a (literal) `%SystemDrive%/…` directory HOT 7
- Get-Help shows incorrect spacing and unwanted prefixes for first line of .EXAMPLE text HOT 6
- Argument with colon in it is incorrectly handled if not quoted HOT 12
- Get-AuthenticodeSignature needs to use embedded signatures HOT 5
- Save-PSResource doesn't install the correct version. HOT 3
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.