PSThreadJob icon indicating copy to clipboard operation
PSThreadJob copied to clipboard

$PSScriptRoot is not populated for scripts run in a ThreadJob

Open jazzdelightsme opened this issue 5 years ago • 0 comments

The problem is pretty straightforward: I have a script that uses $PSScriptRoot, but when I ran the script in a ThreadJob, it malfunctioned, because $PSScriptRoot evaluated to an empty string.

Repro steps:

echo 'Write-Host "PSScriptRoot is: $PSScriptRoot"' > ReproThreadJobHasNoScriptRoot.ps1
start-threadjob -FilePath .\ReproThreadJobHasNoScriptRoot.ps1 | Receive-Job -Wait

Expected result:

PSScriptRoot is: <it should print out the current directory>

Actual result:

PSScriptRoot is:

Environment

Name                           Value
----                           -----
PSVersion                      7.1.0-preview.7
PSEdition                      Core
GitCommitId                    7.1.0-preview.7
OS                             Microsoft Windows 10.0.20222
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

jazzdelightsme avatar Sep 27 '20 05:09 jazzdelightsme