So I have an odd issue and @IISResetMe said to file a bug report so I have. This came about because I changed my VS Code to use pwsh 7 as default, which is a good thing but lead to this issue.
I was running Pester checks against the dbachecks module as I do frequently. The code that is failing was written 2/3 years ago (by someone else!) and hasn't been touched since. Certainly, if I was re-writing it, there are better ways to do it, but it shows what we think is a change in scope in the ForEach method between preview-2 (where Mathias could not reproduce the issue) and rc1 where I can
$Pattern = 'SPN'
@(Get-Content PATHTOFILE.json | Out-String | ConvertFrom-Json).ForEach{
$output = $psitem | Where-Object {
$_.Group -match $Pattern -or $_.Description -match $Pattern -or
$_.UniqueTag -match $Pattern -or $_.AllTags -match $Pattern -or $_.Type -match $Pattern
}
}
$output


There is no output

but this works as expected
$Pattern = 'SPN'
$output = @(Get-Content C:\Users\mrrob\AppData\Local\dbachecks\checks.json | Out-String | ConvertFrom-Json).ForEach{
$psitem | Where-Object {
$_.Group -match $Pattern -or $_.Description -match $Pattern -or
$_.UniqueTag -match $Pattern -or $_.AllTags -match $Pattern -or $_.Type -match $Pattern
} | Select -Last 1
}
$output

pwsh 7.0.0-rc.1> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.0-rc.1
PSEdition Core
GitCommitId 7.0.0-rc.1
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Less typing
@(Get-Content C:\Users\mrrob\AppData\Local\dbachecks\checks.json |
Out-String | ConvertFrom-Json).Where({
$_.Group, $_.Description, $_.UniqueTag, $_.AllTags, $_.Type -match 'spn'
},Last,1)
Can you provide a simple repro I can run without the external file? For example, I thought this would be equivalent, but your issue doesn't repro for me:
@(1,2,3).ForEach{ $a = $_ | ? { $_ -gt 2 } }; $a
Perhaps it's an issue in the ConvertFrom-Json output itself?
The issue is that $output variable is first defined in ForEach script block and the block now does seems not executed in _current_ scope.
It seems we discussed this but I don't remember the PR.
Looks like a breaking change. I set the label until we find a PR where we made the change.
It's interesting sometimes, when something stops working and you look at the old code and wonder how it ever worked! This is one of those cases. (I blame Chrissy! even though it worked in 3,4,5.1 and 6)
It is the number of objects within the array that has changed in 7

So of course in 7.0.0-rc.1 the value of $output is $null unless it matches the last object

Apologies, the title is incorrect, as is the premise of the issue although there is still a difference in functionality. I'll leave better people than I to decide how to proceed and go and re-write a function ;-)
It is the number of objects within the array that has changed in 7
I think it works correctly now. If you think no please open new issue with repo steps.
Yeah, looks like this is the result of the change in how convertfrom-json enumerates by default, no?
It looks like there is a reasonable workaround here that functions the same in both old and new versions though!
Since .Net Core team develop new Json API I believe we will migrate to it in next milestone and I guess there would be some breaking changes again in PowerShell.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
Most helpful comment
Yeah, looks like this is the result of the change in how convertfrom-json enumerates by default, no?
It looks like there is a reasonable workaround here that functions the same in both old and new versions though!