问题
This returns nothing:
$x = "C:\temp"
Start-Job -ScriptBlock {get-childItem -path $x} | Wait-Job | Receive-job
But providing the path parameter without a variable, like so...
Start-Job -ScriptBlock {get-childItem -path C:\temp} | Wait-Job | Receive-job
...returns the contents of that temp folder, durrr.txt in this case. This is on Windows Server 2008R2 SP1 with Powershell $host.version output as follows:
Major Minor Build Revision
----- ----- ----- --------
3 0 -1 -1
Suspecting Powershell's v3, I tried updating a Windows 7 SP1 desktop from v2 to v3, but no luck recreating the problem. On that upgraded desktop, $host.version output now matches the above.
What's going on?
EDIT / What was going on?
The busted job seems equivalent to
Start-Job -ScriptBlock {get-childItem -path $null} | Wait-Job | Receive-job
So gci returned results for the background job's current directory, the Documents folder, which happened to be empty.
回答1:
You need to pass argument to the scriptblock
$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
来源:https://stackoverflow.com/questions/15161115/no-receive-job-results-for-gci-when-path-is-a-variable