question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In background jobs, passing a [scriptblock]::Create()-created script block to a cmdlet fails

See original GitHub issue

Prerequisites

Steps to reproduce

The symptom seems limited to:

  • using Start-Job rather than Start-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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Mar 17, 2023

Good point, @JTDotNet, it is currently permitted with Start-ThreadJob, but it doesn’t work reliably:

  • serialization is not involved in thread-based parallelism (which also includes ForEach-Object -Parallel)
  • it shouldn’t actually be allowed, because the runspace affinity of script blocks can cause state corruption; 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 #12378
1reaction
mklement0commented, Mar 16, 2023

Good point, @SeeminglyScience, thanks.

I’m closing this; should be tagged as a duplicate.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found