Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start a .exe in the background with parameters in a powershell script

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.

like image 490
mles Avatar asked Apr 02 '13 15:04

mles


People also ask

How do I Run an exe file from a PowerShell parameter?

You can run exe files in powershell different ways. For instance if you want to run unrar.exe and extract a . rar file you can simply write in powershell this: $extract_path = "C:\Program Files\Containing folder"; $rar_to_extract = "C:\Path_to_arch\file.

How do I Run a .exe file in the background?

Select the Start button and scroll to find the app you want to run at startup. Right-click the app, select More, and then select Open file location. With the file location open, press the Windows logo key + R, type shell:startup, then select OK.

How do I Run a PowerShell script in the background?

How do I run a PowerShell command in the background? To run in the background, Start-Job runs in its own session in the current session. When you use the Invoke-Command cmdlet to run a Start-Job command in a session on a remote computer, Start-Job runs in a session in the remote session.

What command would you use to Start a job that was coordinated by your computer but whose contents were processed by remote computers?

Use the Invoke-Command cmdlet to run a Start-Job command on a remote computer.


1 Answers

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"
like image 195
Andy Arismendi Avatar answered Oct 17 '22 03:10

Andy Arismendi