I need to start a process from a powershell script and pass such params : -a -s f1d:\some directory\with blanks in a path\file.iss to do that, I write the folowing code :
$process = [System.Diagnostics.Process]::Start("$setupFilePath", '-a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"')
$process.WaitForExit()
as a result the process starts but the last argument : -f1d:\some directory\with blanks in a path\file.iss is not passing correctly. Help, please
In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes (""). Also try using the Grave Accent Character (`) PowerShell uses the grave accent (`) character as its escape character. Just add it before each space in the file name.
-ArgumentListSpecifies parameters or parameter values to use when this cmdlet starts the process. Arguments can be accepted as a single string with the arguments separated by spaces, or as an array of strings separated by commas.
I think you can use Start-Process
:
Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s','-f1"d:\some directory\with blanks in a path\fileVCCS.iss"' |
Wait-Process
I understand your question to be: How to pass multiple arguments to start a process where one of the arguments has spaces?
I'm assuming the equivalent in a Windows batch file would be something like:
"%setupFilePath%" -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
where the double quotes allow the receiving process (setupFilePath in this case) to receive three arguments:
-a
-s
-f1"d:\some directory\with blanks in a path\fileVCCS.iss"
To accomplish this with the code snippet in your question I would use back ticks (to the left of the 1 and below the escape key, not to be confused with a single quote; aka Grave-accent) to escape the inner double quotes like this:
$process = [System.Diagnostics.Process]::Start("$setupFilePath", "-a -s -f1`"d:\some directory\with blanks in a path\fileVCCS.iss`"")
$process.WaitForExit()
Note that in addition to using back ticks I also changed the single quotes around your argument list to double quotes. This was necessary because single quotes do not allow the escapes we need here (http://ss64.com/ps/syntax-esc.html).
Aaron's answer should work just fine. If it doesn't then I would guess that setupFilePath is not interpretting -f1"d:\space here\file.ext"
as you expect it to.
OPINION ALERT The only thing I would add to his answer is to suggest using double quotes and back ticks in order to allow using a variable within the path for argument -f1
:
Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s',"-f1`"$pathToVCCS`"" |
Wait-Process
This way you won't have a hard-coded, absolute path in the middle of a long line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With