Skip to content

pwsh 7.0.0-rc.1 - Scoping in ForEach method change ? #11522

@SQLDBAWithABeard

Description

@SQLDBAWithABeard

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

Steps to reproduce

$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

In Windows PowerShell

image

In PowerShell Core 6.2.3

image

Actual behavior in PowerShell 7.0.0-rc.1

There is no output

image

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

image

Environment data

 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions