Thanks for your answer, it did not work out of the box for me. But it set me in the right direction, gave me some new energy to search for the solution, and made me realize a few things.
The first thing I got to work as expected was:
param($directory)
start-job -Name $directory -scriptblock {dir $args > C:\$args.txt} -ArgumentList $directory
Then I realized that the script I really want to do needs more than one parameter put into the script block. Trying :
param($directory, $outfile)
start-job -Name $directory -scriptblock {dir $args > C:\$args.txt} -ArgumentList $directory,$outfile
That did not work as expected. The resulting $args was concatenations of $directory and $outfile Also tried args[0] and args[1], but no.
Then I found: $using:variable that I used like this:
param($directory, $outfile)
start-job -Name $directory -scriptblock {dir $using:directory > c:\$using:outfile.txt}
That gave me the result I needed.
So far so good. But it didn’t end here. The script that I want to do shall run a node.js script requiring parameters (very much simplified) like this:
( $topic is not a variable btw )
node script -p '$topic/sometopic/+/+/name' -p '$topic/someothertopic/resource/#'
And I want a Powershell script that I can run like this:
./MyPSScript.ps1 sometopic someothertopic
I Tried:
param($sometopic, $someothertopic)
start-job -Name $sometopic -ScriptBlock{node script.js -p '$topic/$using:sometopic/+/+/name' -p '$topic/$using:someothertopic/recources/#'}
Did not work.
Finally, the solution was two Powershell scripts.
First script1.ps1:
./script1.ps1 sometopic someothertopic
Like this:
param($sometopic, $someothertopic)
$tmp="!topic/" + $sometopic + "/+/+/name"
$parm1=$tmp.replace('!', '$')
$tmp="!topic/" + $someothertopic + "/recources/#"
$parm2=$tmp.replace('!', '$')
# Calling a second script.
./script2.ps1 $parm1 $parm2
And script2.ps1 doing:
param($param1, $param2)
start-job -Name $param1 -ScriptBlock{node script -p $using:param1 -p $using:param2}
And this (and a lot more in the actual real script) work as expected
Is this the right way to do it or is there a better way?