1

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.

noam
  • 1,914
  • 2
  • 20
  • 26

1 Answers1

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
CB.
  • 58,865
  • 9
  • 159
  • 159
  • Hallelujah, thx. But why the different behavior on Win7 vs 2008R2? – noam Mar 01 '13 at 15:28
  • Your code doesn't work either in powershell v2.0/v3.0 on xp, 7, 2008, 2008 r2. The only way to pass variable in a sciptblock is using argumentlist paramenter. In v3 I think you can use `-path $using:x` and it works. – CB. Mar 01 '13 at 15:31
  • The first two lines in my question do return results on Win7. I'll read up on scriptblock arguments. – noam Mar 01 '13 at 15:50
  • 1
    @noam Try ook more accurately and you'll find the result isn't the dir of you c:\temp. In my 7 box returns dir of %env:userprofile\documents ... – CB. Mar 01 '13 at 16:45
  • D'oh. I tried down-voting myself, but thankfully that is not allowed. – noam Mar 01 '13 at 17:44