在Powershell脚本中使用参数在后台启动.exe [英] start a .exe in the background with parameters in a powershell script

查看:548
本文介绍了在Powershell脚本中使用参数在后台启动.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,通常在powershell中这样启动:

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"

但没有成功.此外,它还应在Powershell脚本中运行.

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

推荐答案

该作业将是PowerShell.exe的另一个实例,并且不会在同一路径下启动,因此.无法正常工作.它需要知道storage.exe在哪里.

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.

此外,您还必须使用脚本块中自变量列表中的自变量.您可以使用内置的args数组,也可以使用命名参数. args方式需要最少的代码.

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"

这篇关于在Powershell脚本中使用参数在后台启动.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆