Bug in PowerShell version 5.1.17134.165 $Using: fails when used interactively in Start-Job

%3CLINGO-SUB%20id%3D%22lingo-sub-217835%22%20slang%3D%22en-US%22%3EBug%20in%20PowerShell%20version%205.1.17134.165%20%24Using%3A%20fails%20when%20used%20interactively%20in%20Start-Job%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-217835%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20following%20script%20fails%20when%20used%20interactively%20(F8%20in%20ISE%20or%20pasted%20in%20the%20console)%20on%20Windows%2010%201803%3B%3C%2FP%3E%3CP%3E%24FilePath%20%3D%20%22C%3A%5CTemp%5CJobTest2.txt%22%3CBR%20%2F%3E%24Job%20%3D%20Start-Job%20-ScriptBlock%20%7BOut-File%20-InputObject%20%24args%20-FilePath%20%24Using%3AFilePath%7D%20-ArgumentList%20%24FilePath%26nbsp%3B%3CBR%20%2F%3E%24Job%20%7CWait-Job%20%7CReceive-Job%20-Keep%3CBR%20%2F%3EGet-Content%20%24FilePath%3C%2FP%3E%3CP%3EIt%20works%20up%20to%201709%20(PS%205.1.16299.492)%20and%20is%20working%20in%206.0.2%20Core.%20In%201803%20if%20the%20script%20is%20saved%20and%20called%20it%20will%20work.%3C%2FP%3E%3CP%3EI%20have%20also%20found%20a%20work%20around%2C%20if%20you%20add%20%22%3B%24null%22%20after%20%22%24Using%3AFilePath%22%20(it%20also%20works%20specifying%20other%20variables)%20e.g.%3C%2FP%3E%3CP%3E%3CSPAN%3E%24Job%20%3D%20Start-Job%20-ScriptBlock%20%7BOut-File%20-InputObject%20%24args%20-FilePath%20%24Using%3AFilePath%20%3B%24null%7D%20-ArgumentList%20%24FilePath%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

The following script fails when used interactively (F8 in ISE or pasted in the console) on Windows 10 1803;

$FilePath = "C:\Temp\JobTest2.txt"
$Job = Start-Job -ScriptBlock {Out-File -InputObject $args -FilePath $Using:FilePath} -ArgumentList $FilePath 
$Job |Wait-Job |Receive-Job -Keep
Get-Content $FilePath

It works up to 1709 (PS 5.1.16299.492) and is working in 6.0.2 Core. In 1803 if the script is saved and called it will work.

I have also found a work around, if you add ";$null" after "$Using:FilePath" (it also works specifying other variables) e.g.

$Job = Start-Job -ScriptBlock {Out-File -InputObject $args -FilePath $Using:FilePath ;$null} -ArgumentList $FilePath

0 Replies