Repository navigation
Background job operator does not work when invoking a script block variable #9703
Copy link
Copy link
Closed
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime
Description
When a variable with a script block is passed to a background job via using, the variable inside the job is a string instead of a script block.
As Bruce Payette (@bpayette) indicated below, this was by design to reduce the risk of accidentally executing code; however, this clearly seems like a design flaw now that we have the & background operator in PowerShell, and while it can be worked around, any code that works around it should be unnecessary. It is worth mentioning that the workaround demonstrated below would continue to work even if this bug was fixed because objects of type scriptblock convert implicitly to string, so the likelihood of this causing an issue for folks if it was fixed should be very low.
Steps to reproduce
Invoke-Pester -ScriptBlock {
Describe 'Tests for using variables of specific types' -Tags 'CI' {
It 'Implicit using with background job operator and variable of type ScriptBlock' {
$sb = {Get-Process -Id $pid}
$job = & $sb &
$results = $job | Receive-Job -Wait
$results | Should -BeOfType PSObject
$results.PSTypeNames | Should -Contain Deserialized.System.Diagnostics.Process
}
}
}Expected behavior
Tests Passed: 1, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0
Actual behavior
Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0
Workaround
Invoke-Pester -ScriptBlock {
Describe 'Tests for using variables of specific types' -Tags 'CI' {
It 'Implicit using with background job operator and variable of type ScriptBlock' {
$sb = {Get-Process -Id $pid}
$job = & ([scriptblock]::Create($sb)) &
$results = $job | Receive-Job -Wait
$results | Should -BeOfType PSObject
$results.PSTypeNames | Should -Contain Deserialized.System.Diagnostics.Process
}
}
}Environment data
Name Value
---- -----
PSVersion 6.2.0
PSEdition Core
GitCommitId 6.2.0
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime
Activity
[-]Background job operator does not work when invoking a script block[/-][+]Using variable feature does not work properly with scriptblock variables[/+]bpayette commentedon May 24, 2019
This is (or was) by design. IIRC we serialize scriptblocks to strings in flight to reduce the risk of accidentally executing code. The problem was that, when trying to secure a remote endpoint, there were so many places that scriptblock parameters could automatically get executed we just blocked everything. Since you can explicitly turn a string into a scriptblock with
[ScriptBlock]::Create()(but not with a cast!) it was viewed as being not too onerous. Of course this doesn't really make sense for background jobs so maybe we should reconsider our strategy here :-)KirkMunro commentedon May 24, 2019
Thanks for the background, Bruce Payette (@bpayette), that makes sense. I agree the strategy doesn't seem appropriate when running a background job that invokes an anonymous function.
KirkMunro commentedon Jun 13, 2019
I'm going to update the title and description so that this focuses specifically on use of the
&background operator, since that is the part of the design that needs revisiting.[-]Using variable feature does not work properly with scriptblock variables[/-][+]Background job operator does not work when invoking a script block variable[/+]microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.