Skip to content

Background job operator does not work when invoking a script block variable #9703

Closed

Description

@KirkMunro

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

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on May 23, 2019
  2. changed the title [-]Background job operator does not work when invoking a script block[/-] [+]Using variable feature does not work properly with scriptblock variables[/+] on May 23, 2019
  3. bpayette commented on May 24, 2019

    @bpayette
    Contributor

    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 :-)

  4. KirkMunro commented on May 24, 2019

    @KirkMunro
    ContributorAuthor

    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.

  5. KirkMunro commented on Jun 13, 2019

    @KirkMunro
    ContributorAuthor

    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.

  6. changed the title [-]Using variable feature does not work properly with scriptblock variables[/-] [+]Background job operator does not work when invoking a script block variable[/+] on Jun 13, 2019
  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  10. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally 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 moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions