2014-09-13 58 views
0
$commonElementsLocation = ((Get-Location).Path + "\FolderMatchingCommonWords.txt") 

if ($commonElementsLocation) { 
    $result += Start-Job -InitializationScript { 
        $commonElements = Get-Content -Path $commonElementsLocation 
       } -ScriptBlock $testScriptBlock -ArgumentList "testing" | Wait-Job | Receive-Job 
} 

不知道我在做什麼錯在這裏 - 可能是一個愚蠢的錯誤,但我把周圍的啓動工作語句的條件,和PowerShell還提供了以下錯誤:PowerShell中獲取內容的路徑無效錯誤

Get-Content : Cannot bind argument to parameter 'Path' because it is null. 
At line:1 char:39 
+ $commonElements = (Get-Content -Path $commonElementsLocation) 
+          ~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidData: (:) [Get-Content], ParameterBindingValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentC 
    ommand 

Running startup script threw an error: Cannot bind argument to parameter 'Path' because it is null.. 
    + CategoryInfo   : OpenError: (localhost:String) [], RemoteException 
    + FullyQualifiedErrorId : PSSessionStateBroken 

回答

1

在使用V3加$:

....(Get-Content -Path $using:commonElementsLocation... 
+0

正確。作業在完全獨立的PowerShell進程中運行。無論init腳本使用什麼「外部」變量,都應該使用'$ using:'引用它們,以便它們序列化到運行作業的PowerShell會話。 – 2014-09-13 21:55:49

0

您已經使用一個變量的腳本塊一次與$testScriptBlock。您可以再次爲-InitializationScript做到這一點?

$anotherSB = [scriptblock]::create("$commonElements = (Get-Content -Path $commonElementsLocation)") 
$result += Start-Job -InitializationScript $anotherSB -ScriptBlock $testScriptBlock -ArgumentList "testing" | Wait-Job | Receive-Job