7

I have a programm which i usually start like this in powershell:

.\storage\bin\storage.exe -f storage\conf\storage.conf

What is the correct syntax for calling it in the background? I tried many combinations like:

start-job -scriptblock{".\storage\bin\storage.exe -f storage\conf\storage.conf"}
start-job -scriptblock{.\storage\bin\storage.exe} -argumentlist "-f", "storage\conf\storage.conf"

but without success. Also it should run in a powershell script.

mles
  • 4,534
  • 10
  • 54
  • 94

1 Answers1

9

The job will be another instance of PowerShell.exe and it will not start in same path so . won't work. It needs to know where storage.exe is.

Also you have to use the arguments from argumentlist in the scriptblock. You can either use the built-in args array or do named parameters. The args way needs the least amount of code.

$block = {& "C:\full\path\to\storage\bin\storage.exe" $args}
start-job -scriptblock $block -argumentlist "-f", "C:\full\path\to\storage\conf\storage.conf"

Named parameters are helpful to know what what arguments are supposed to be. Here's how it would look using them:

$block = {
    param ([string[]] $ProgramArgs)
    & "C:\full\path\to\storage\bin\storage.exe" $ProgramArgs
}
start-job -scriptblock $block -argumentlist "-f", "C:\full\path\to\storage\conf\storage.conf"
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • 1
    How would the named parameter version look like? I am already using $args for calling the script, so I can't use the args array. – mles Apr 02 '13 at 15:38
  • You can still use args. It has a new scope in the scriptblock (and it will be used in another instance of powershell.exe for the job) but I updated to show named parameters. – Andy Arismendi Apr 02 '13 at 15:53
  • Ah Ok. Now I have run into another problem. You might know this one too? http://stackoverflow.com/questions/15769126/expand-variable-in-block – mles Apr 02 '13 at 16:20