Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

How to run an executable (exe) by providing a config file in powershell

I'm trying to run an exe in the background by providing a config file (yml in my case)

Tried the below, however this is not pushing the execution to background. -

./my.exe start --config-file $my_config_file

Found 'start-process' command which are specifically used for this case. With argument list is there any way to send the config file?

Start-Process -Wait -FilePath "my.exe" -ArgumentList 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove the -Wait argument and pass the process arguments as an array via -ArgumentList parameter:

Start-Process -FilePath "my.exe" -ArgumentList 'start', '--config-file', "`"$my_config_file`""

The strange quoting for $my_config_file is required because a path may contain spaces. Start-Process does not do automatic quoting. From the docs:

If parameters or parameter values contain a space, they need to be surrounded with escaped double quotes.

Note that you won't receive output of the started process, if that matters to you. You can redirect to a file, using parameters -RedirectStandardOutput and -RedirectStandardError, but you can't (easily) store the output in a variable.

A way to start a process in the background, while being able to receive its output, is to create a job.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...