Repository navigation
Open
Labels
Issue-BugIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-NeedsReviewNeeds a review by the labeled Working Group
Description
Note:
- The bug equally affects
Start-ThreadJob.
In the script block passed to the (implied as the 1st positional) -ScriptBlock parameter of Start-Job, it is possible to reference variables from the calling scope via the $using: scope; e.g.:
# Make the background job's working directory the same as the calling session's and
# print the then-current location.
Start-Job { Set-Location $using:PWD; $PWD } | Receive-Job -Wait -AutoRemoveBy contrast, the following - even though I'd expect it to be equivalent - does NOT work, because the $using: reference is in the -InitializationScript script block:
Start-Job -InitializationScript { Set-Location $using:PWD } { $PWD } | Receive-Job -Wait -AutoRemoveInvocation fails with the following error messages:
A Using variable cannot be retrieved. A Using variable can be used only with
Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with
Invoke-Command, the Using variable is valid only if the script block is invoked on
a remote computer.
....
Note that the above command would be the cleanest workaround for not having a -WorkingDirectory parameter (see #4287) to set the working directory explicitly.
Both script blocks execute in the job's session, so they should both support $using: variable references.
Environment data
PowerShell Core v6.0.0-beta.5 on macOS 10.12.6
PowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)Reacted by Mark Kraus, Joel Bennett, Brant Bobby, Jean-Pierre van Riel, Kris Borowinski, Kirk Munro, Steffen Kampmann, Mon, mkht, Mika K. and 1 more
Reacted by Kris Borowinski
Metadata
Metadata
Assignees
Labels
Issue-BugIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-NeedsReviewNeeds a review by the labeled Working Group
Activity
iSazonov commentedon Aug 9, 2017
Jason Shirk (@lzybkr) Could you please comment - Is this a bug or an enhancement?
lzybkr commentedon Aug 9, 2017
It looks like an oversight, so I'll call it a bug.
brantb commentedon Dec 13, 2017
Possibly obvious statement: This affects PS 5.1 as well.
mklement0 commentedon Mar 18, 2019
Thanks, Brant Bobby (@brantb); additionally, in Windows PowerShell it also affects
Register-ScheduledJob(which doesn't ship with PowerShell Core, at least as of PowerShell Core 6.2.0-rc.1).JustinGrote commentedon Feb 9, 2022
Paul Higinbotham (@PaulHigin) any interest here? It's really hard to initialize the state of a job when you can't bring that state in from the caller's scope.
PaulHigin commentedon Feb 9, 2022
I agree this should be fixed. Not sure when I can get to it, but at least it is back on my radar. Thanks for bringing it to my attention.
JustinGrote commentedon Feb 9, 2022
Thanks. To others as a workaround, you can append your init script to the main block and still be able to use USING
Knud3 commentedon Apr 21, 2023
Nice workaround Justin Grote (@JustinGrote)
Here is working example of it:
Or assign variable again:
And you can also use it as function:
Or whole thing shorter:
Output
mon-jai commentedon Feb 5, 2024
Please don't close it.