Command line arguments for msiexec break on powershell if they contain space

帅比萌擦擦* 提交于 2019-12-10 03:28:51

问题


I'm trying to set a public property in an installshield installer with a value containing space. While running the MSI installer I'm using below command on PowerShell command prompt. Since the value contains spaces so I used double quotes to pass the value

msiexec -i "myinstaller.msi" MYDIRPATH="C:\new folder\data.txt"

It breaks the command as the argument value C:\new folder\data.txt has a space in the string new folder and results in below error prompt of msiexec which is suggestive of the fact that arguments passed to the msiexec command is problematic:

If I run the very same command on windows default command shell prompt then it works like a charm.

The options that I've tried:

  1. Using single quote in place of double quotes
  2. Using a back tick (`) character before space in the argument as per this answer.

回答1:


Try with this

msiexec -i "myinstaller.msi" MYDIRPATH=`"C:\new folder\data.txt`"

The escape character in PowerShell is the grave-accent(`).




回答2:


To complement Marko Tica's helpful answer:

Calling external utilities in PowerShell is notoriously difficult, because PowerShell, after having done its own parsing first, rebuilds the command line that is actually invoked behind the scenes, and it's far from obvious what rules are applied.

To help with this problem, PSv3+ offers --%, the stop-parsing symbol, which is the perfect fit here, given that the command line contains no references to PowerShell variables or subexpressions: It passes the rest of the command line as-is to the external utility, save for potential expansion of %...%-style environment variables:

 # Everything after --% is passed as-is.
 msiexec --% -i "myinstaller.msi" MYDIRPATH="C:\new folder\data.txt"

As for what actually happened in your original attempt:

PowerShell translated
MYDIRPATH="C:\new folder\data.txt" into
"MYDIRPATH=C:\new folder\data.txt" behind the scenes - note how the entire token is now enclosed in "...".

Arguably, these two forms should be considered equivalent by msiexec, but all bets are off in the anarchic world of Windows command-line argument parsing.




回答3:


This is the best way to install a program in general with Powershell.

Here's an example from my own script:

start-process "c:\temp\SQLClient\sqlncli (x64).msi" -argumentlist "/qn IACCEPTSQLNCLILICENSETERMS=YES" -wait

Use Start-Process "Path\to\file\file.msi or .exe" -argumentlist (Parameters) "-qn or whatever" -wait.

Now -wait is important, if you have a script with a slew of programs being installed, the wait command, like piping to Out-Null, will force Powershell to wait until the program finishes installing before continuing forward.



来源:https://stackoverflow.com/questions/44498335/command-line-arguments-for-msiexec-break-on-powershell-if-they-contain-space

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