1

这将返回什么:没有收到作业结果GCI时-path是一个变量

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

但是,如果没有一个变量提供路径参数,像这样......

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

.. 。在这种情况下,返回该临时文件夹的内容durrr.txt。这是基于Windows Server 2008 R2 SP1使用PowerShell $ host.version输出如下:

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

怀疑PowerShell的V3,我试着从V2到V3更新的Windows 7 SP1桌面,但没有运气重建的问题。在该升级的桌面上,$ host.version输出现在与上述相匹配。

发生了什么事?

编辑/这是怎么回事?

的破获工作似乎等同于

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

所以GCI返回后台作业的当前目录的结果,文档文件夹,这正好是空的。

回答

1

你需要传递参数给脚本块

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

在PowerShell中V3,你可以这样做:

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

哈利路亚,THX。但为什么在Win7和2008R2上的不同行为? – noam 2013-03-01 15:28:55

+0

你的代码在xp,7,2008,2008 r2的PowerShell v2.0/v3.0中不起作用。在sciptblock中传递变量的唯一方法是使用参数列表参数。在v3中,我认为你可以使用'-path $ using:x'并且它可以工作。 – 2013-03-01 15:31:34

+0

我的问题中的前两行确实会在Win7上返回结果。我会阅读scriptblock参数。 – noam 2013-03-01 15:50:24