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