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

混江龙づ霸主 提交于 2019-12-10 13:24:38

问题


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.


回答1:


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"


来源:https://stackoverflow.com/questions/15767657/start-a-exe-in-the-background-with-parameters-in-a-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!