johnnygear
johnnygear

Reputation: 93

powershell get-content path null error

$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
}

Not sure what I am doing wrong here - probably a stupid mistake, but I put a condition around the Start-Job statement, and powershell still gives the following error:

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

Upvotes: 0

Views: 1652

Answers (2)

Matt
Matt

Reputation: 46710

You have already used a variable for a script block once with $testScriptBlock. You could just do that again for -InitializationScript?

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

Upvotes: 0

walid toumi
walid toumi

Reputation: 2272

In v3 add $using:

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

Upvotes: 1

Related Questions