There are no reception job results for gci when -path is a variable

This does not return anything:

$x = "C:\temp" Start-Job -ScriptBlock {get-childItem -path $x} | Wait-Job | Receive-job 

But providing a path parameter without a variable, for example ...

 Start-Job -ScriptBlock {get-childItem -path C:\temp} | Wait-Job | Receive-job 

... returns the contents of this temporary durrr.txt folder in this case. This applies to Windows Server 2008R2 SP1 with Powershell $ host.version output as follows:

 Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1 

Suspecting Powershell v3, I tried updating the Windows 7 desktop with Service Pack 1 (SP2) from version 2 to version 3, but failed to recreate the problem. On this updated desktop, the output of $ host.version now matches the above.

What's happening?

EDIT / What is happening?

Overloaded task seems equivalent

 Start-Job -ScriptBlock {get-childItem -path $null} | Wait-Job | Receive-job 

So, gci returned the results for the current directory of the current job, the Documents folder, which turned out to be empty.

+1
source share
1 answer

You need to pass an argument to the script block

 $x = "C:\temp" Start-Job -ScriptBlock {get-childItem -path $args[0]} -argumentlist $x | Wait-Job | Receive-job 

In powershell V3 you can do:

 $x = "C:\windows" Start-Job -ScriptBlock {get-childItem -path $using:x} | Wait-Job | Receive-job 
+1
source

Source: https://habr.com/ru/post/1489810/


All Articles