GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix using variable for nested foreach parallel calls by PaulHigin · Pull Request #14548 · PowerShell/PowerShell

Conversation

@PaulHigin

@PaulHigin Paul Higinbotham (PaulHigin) commented Jan 5, 2021

Copy link
Copy Markdown
Contributor

PR Summary

This fixes a bug in ForEach-Object -Parallel where a using variable in a nested ForEach-Object -Parallel throws an error even when the variable is defined in the correct scope (Issue #11817).

PR Context

This error was occurring because, when ForEach-Object -Parallel was assembling the user variable map, it was searching all nested scriptblocks within the ForEach scriptblock, with the result of finding nested using variables where the variable had not yet been defined. Since the nested scriptblock using variable had not been defined in the current scope, a mapping error was thrown.

Fix is to change the using variable map function to not search nested scriptblocks in the ForEach -Parallel case. This way foreach -parallel using variable mapping is always performed only for the current scope.

Many thanks to Michael Klement (@mklement0) for pointing out the fix.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: or [ WIP ] to the beginning of the title (the WIP bot will keep its status check at Pending while the prefix is present) and remove the prefix when the PR is ready.
  • Breaking changes
    • None
    • OR
    • Experimental feature(s) needed
      • Experimental feature name(s):
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed:
  • Testing - New and feature
    • N/A or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed
  • Tooling
    • I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    • OR
    • I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
      • Impact on PowerShell Editor Services which is used in the PowerShell extension for VSCode (which runs in a different PS Host).
      • Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
      • Impact on PSScriptAnalyzer (which provides linting & formatting in the editor extensions).
      • Impact on EditorSyntax (which provides syntax highlighting with in VSCode, GitHub, and many other editors).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@PaulHigin
Fix using variable for nested foreach parallel calls
838b150
@ghost ghost assigned Aditya Patwardhan (adityapatwardhan) Jan 5, 2021
@PaulHigin Paul Higinbotham (PaulHigin) mentioned this pull request Jan 5, 2021
[WIP] Fix using variable error for nested foreach parallel #11829
Closed
8 of 14 tasks
@PaulHigin
Paul Higinbotham (PaulHigin) requested a review from Dongbo Wang (daxian-dbw) January 5, 2021 22:12
@PaulHigin
Fix some codacy issues
071cdd3
@iSazonov Ilya (iSazonov) added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log BackPort-7.1.x-Consider labels Jan 6, 2021
@chasewilson Chase Wilson (chasewilson) mentioned this pull request Jan 7, 2021
ForEach-Object -Parallel cmdlet needs clarification about using variable scope MicrosoftDocs/PowerShell-Docs#5436
Closed
2 of 11 tasks
@PaulHigin

Paul Higinbotham (PaulHigin) commented Jan 7, 2021

Copy link
Copy Markdown
Contributor Author

Dongbo Wang (@daxian-dbw) Can you please review these changes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@PaulHigin
Paul Higinbotham (PaulHigin) requested a review from Steve Lee (SteveL-MSFT) January 12, 2021 17:14
SteveL-MSFT
Steve Lee (SteveL-MSFT) approved these changes Jan 12, 2021
View reviewed changes

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment, otherwise looks fine to me

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/System.Management.Automation/engine/runtime/ScriptBlockToPowerShell.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@PaulHigin
Add comment about expanding alias list in the future
1e465c8
@ghost ghost added the Review - Needed The PR is being reviewed label Jan 21, 2021
@ghost

Deleted user (ghost) commented Jan 21, 2021

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@PaulHigin Paul Higinbotham (PaulHigin) assigned Andrew (anmenaga) and unassigned Aditya Patwardhan (adityapatwardhan) Jan 21, 2021
@anmenaga

Andrew (anmenaga) commented Jan 28, 2021

Copy link
Copy Markdown

Restarted CIs for what looks like unrelated failures in Update-Help tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost removed the Review - Needed The PR is being reviewed label Jan 28, 2021
@anmenaga

Andrew (anmenaga) commented Jan 28, 2021

Copy link
Copy Markdown

Paul Higinbotham (@PaulHigin) it would be good to fix CodeFactor violations in the new code that is added; thank you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Paul Higinbotham (PaulHigin) added 2 commits January 28, 2021 08:34
@PaulHigin
Address CodFactor warnings
1d3d2d5
@PaulHigin
more nonsense
14476e4
@anmenaga

Andrew (anmenaga) commented Feb 1, 2021

Copy link
Copy Markdown

Restarted CIs again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anmenaga

Andrew (anmenaga) commented Feb 2, 2021

Copy link
Copy Markdown

One more time restarted CIs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anmenaga
Andrew (anmenaga) merged commit 0039807 into PowerShell:master Feb 2, 2021
@daxian-dbw Dongbo Wang (daxian-dbw) added this to the 7.2.0-preview.3 milestone Feb 2, 2021
@ghost

Deleted user (ghost) commented Feb 12, 2021

Copy link
Copy Markdown

🎉v7.2.0-preview.3 has been released which incorporates this pull request.:tada:

Handy links:

  • Release Notes
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sdwheeler Sean Wheeler (sdwheeler) mentioned this pull request Feb 18, 2021
Fix using variable for nested ForEach-Object -Parallel calls MicrosoftDocs/PowerShell-Docs#7266
Closed
@adityapatwardhan

Aditya Patwardhan (adityapatwardhan) commented Mar 2, 2021

Copy link
Copy Markdown
Member

Paul Higinbotham (@PaulHigin) - Should this be backported to 7.1.x?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@PaulHigin

Paul Higinbotham (PaulHigin) commented Mar 2, 2021

Copy link
Copy Markdown
Contributor Author

Aditya Patwardhan (@adityapatwardhan) I think the impact of this change is small, since if involves nested foreach calls. So I would say it does not need to be backported as it probably does not affect a lot of users.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adityapatwardhan

Aditya Patwardhan (adityapatwardhan) commented Mar 2, 2021

Copy link
Copy Markdown
Member

Removed the back port label as per comment from Paul Higinbotham (@PaulHigin)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@keystroke

Bryant (keystroke) commented Apr 16, 2021

Copy link
Copy Markdown

This is not unique to parallel foreach, it applies to nested start-job and nested invoke-command calls (double-hop) or any combination of those, like using invoke-command and then running start-job in the remote session, etc. Does this fix work for these scenarios as well (everywhere $using is supported) or was this somehow scoped to parallel foreach?

mklement0 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adityapatwardhan Aditya Patwardhan (adityapatwardhan) removed the BackPort-7.1.x-Consider label Sep 16, 2021
@DarkLite1 DarkLite1 (DarkLite1) mentioned this pull request Feb 23, 2024
For ForEach-Object -Parallel, nested loops improvment #11817
Closed
@surfingoldelephant surfingoldelephant mentioned this pull request Apr 13, 2025
Update ForEach-Object example 17 to reflect current behavior MicrosoftDocs/PowerShell-Docs#11997
Merged
4 tasks done
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@PaulHigin
Fix using variable for nested foreach parallel calls (PowerShell#14548)
10ebd61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@SteveL-MSFT Steve Lee (SteveL-MSFT) SteveL-MSFT approved these changes
@daxian-dbw Dongbo Wang (daxian-dbw) Awaiting requested review from daxian-dbw daxian-dbw is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@anmenaga Andrew (anmenaga)

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Milestone

7.2.0-preview.3

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@PaulHigin @anmenaga @adityapatwardhan @keystroke @SteveL-MSFT @daxian-dbw @iSazonov
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.