Powershell calling a script in a new job

83 Views Asked by At

i am trying to create a new job which calls a script:

Write-Output "start"

$j1 = Start-Job -ScriptBlock { Write-Output "Thread1 running"; & ${PSScriptRoot}\script.ps1 -ARG1 ${arg1} -ARG2 ${arg2}; Write-Output "Thread1 finish" }

$j1 | Wait-Job | Receive-Job

Write-Output "done"

But its not working. The error says (german): Die Benennung "\script.ps1" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.

The problem is obviously in & ${PSScriptRoot}\script.ps1 -ARG1 ${arg1} -ARG2 ${arg2}; but the root path is 100% correct.

Does somebody has a Solution for my problem?

1

There are 1 best solutions below

0
Ranadip Dutta On

So $psscriptRoot\script.ps1 has to be string concatenated which is not there in your case. As a result, PS could not understand it and it was considering the path as C:\Users\xxx\Desktopscript.ps1 instead of C:\Users\xxx\Desktop\script.ps1. Kindly enclose that in the double quotes:

Replace: ${PSScriptRoot}\script.ps1 with "${PSScriptRoot}\script.ps1"

Here is the output:

enter image description here

You should be good with the changes. Note by, that I have not worked on the argument part.