In background jobs, passing a [scriptblock]::Create()-created script block to a cmdlet fails
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
The symptom seems limited to:
- using
Start-Job
rather thanStart-ThreadJob
- only when passing the dynamically created script block to a cmdlet as an argument; by contrast, invoking it with
&
works fine.
# !! Fails, but note that it works when using `Start-ThreadJob` instead, and when using `&` in lieu of `Invoke-Command`
Start-Job { Invoke-Command -ScriptBlock ([scriptblock]::create(' "hi" ')) } |
Receive-Job -Wait -AutoRemoveJob
Expected behavior
hi
Actual behavior
An error occurs (see below).
Error details
Invoke-Command: Cannot bind parameter 'ScriptBlock'. Cannot convert the " "hi" " value of type "System.String" to type "System.Management.Automation.ScriptBlock".
Environment data
PowerShell Core 7.3.0-rc.1 on macOS 12.6
Visuals
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How do I pass a scriptblock as one of the parameters in ...
Here's one way to solve this, pass the scriptblock code as a string, then create a scriptblock from the string inside the job...
Read more >about Script Blocks - PowerShell
In the PowerShell programming language, a script block is a collection of statements or expressions that can be used as a single unit....
Read more >about Jobs - PowerShell
Use the ScriptBlock parameter to specify the command. The following command starts a background job that runs a Get-Process command on the local ......
Read more >Background Job Failing Despite Script Working in Console
I am trying to run a background job and it keeps failing but when I run the same script it works in the...
Read more >PowerShell Background Jobs - Technology short and sweet
To start a background job use Start-Job and pass in the script block you want to execute, PowerShell will immediately return to the...
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 Free
Top 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
Good point, @JTDotNet, it is currently permitted with
Start-ThreadJob
, but it doesn’t work reliably:ForEach-Object -Parallel
)ForEach-Object -Parallel
therefore explicitly disallows referencing script blocks from the caller’s scope with$using
;Start-ThreadJob
just happens to not enforce this constraint, even though it should - see #12378Good point, @SeeminglyScience, thanks.
I’m closing this; should be tagged as a duplicate.