Setting property values with the .ForEach() array method doesn't work with a single [pscustomobject] instance.
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
.ForEach()
has an overload that allows setting property values by name and (array of) property values.
This works in general, but unexpectedly not with a single [pscustomobject]
instance (as later discovered, it does work with arrays of [pscustomobject]
instances).
($obj = [pscustomobject] @{ p = 1 }).ForEach('p', 42)
Expected behavior
No output, and $obj.p
should report 42
afterwards.
Actual behavior
1
- the current property value - is output, and the attempt to assign a new value is ignored.
Error details
No response
Environment data
PowerShell Core 7.3.0-preview.1
Visuals
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Setting property values with the .ForEach() array method ...
Setting property values with the .ForEach() array method doesn't work with a single [pscustomobject] instance. #4182. Sign in to view logs.
Read more >Prevent adding pscustomobject to array if already exists
GetHashCode() on a [pscustomobject] instance always yields the same value, irrespective of the instance's set of properties and values.
Read more >Everything you wanted to know about PSCustomObject
In this article. Creating a PSCustomObject; Working with properties; Adding object methods; Using DefaultPropertySet (the long way); Function ...
Read more >[SOLVED] Changing NoteProperty values in a collection of ...
I have a hashtable with a collection of properties and where the property values are a collection of PSCustomObjects.
Read more >Array.prototype.forEach() - JavaScript - MDN Web Docs
Array.prototype.forEach(). The forEach() method of Array instances executes a provided function once for each array element.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Aside from the second one (which appears to be a very bizarre but separate binding bug) that all looks right.
Where
andForEach
always return aCollection<>
regardless of the input.Looks like the binder is calling
InvokeAdaptedMember
instead ofEnumerableOps.ForEach
.Here’s the
Bind
result for a scalarint
:(side note, dunno what’s up with the double enumerate there)
And here it is for a
pscustomobject
: