My PowerShell script should start an external executable with specified parameters. I have two strings: The file name, and the arguments. This is what process starting APIs usually want from me. PowerShell however fails at it.
I need to keep the executable and arguments in a separate strings because these are configured elsewhere in my script. This question is just about using these strings to start the process. Also, my script needs to put a common base path in front of the executable.
This is the code:
$execFile = "SomeSetup.exe"
$params = "/norestart /verysilent"
& "$basePath\$execFile" $params | Out-Host
# Pipe to the console to wait for it to finish
This is the actual result (does not work with this program):
- Process file name: "C:\My\Base path\SomeSetup.exe"
- Process command line: "/norestart /verysilent"
This is what I'd expect to have (this would work):
- Process file name: "C:\My\Base path\SomeSetup.exe"
- Process command line: /norestart /verysilent
The problem is that the setup recognises the extra quotes and interprets the two arguments as one - and doesn't understand it.
I've seen Start-Process
but it seems to require each parameter in a string[] which I don't have. Splitting these arguments seems like a complicated shell task, not something I'd do (reliably).
What could I do now? Should I use something like
& cmd /c "$execFile $params"
But what if $execFile contains spaces which can well happen and usually causes much more headache before you find it.