在命令行上设置公共属性值

编程入门 行业动态 更新时间:2024-10-20 15:48:04
本文介绍了在命令行上设置公共属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在命令行上设置公共属性值 msi 遵循模式

MyInstaller.msi PUBLICPROPERTY="someValue"

这适用于命令提示符"又名 cmd.exe 和 powershell.

This works on "Command Prompt" aka cmd.exe and powershell.

但是

MyInstaller.msi PUBLICPROPERTY=""

在 powershell 中没有像预期的那样工作.我希望它将 PUBLICPROPERTY 设置为 null,但它将 PUBLICPROPERTY 设置为值 "CURRENTDIRECTORY="C:\temp\msi\""(它确实像 cmd.exe 预期的那样工作).

does not work like expected in powershell. I expected that it sets PUBLICPROPERTY to null but it sets PUBLICPROPERTY to the value "CURRENTDIRECTORY="C:\temp\msi\"" (it does work like expected with cmd.exe).

为什么 powershell 和 cmd.exe 的行为不同,如何解决?

Why do powershell and cmd.exe behaviour different, and how can it be fixed?

推荐答案

PowerShell,在必要的 Windows 上,在幕后对您的论点进行重新引用.

这种不可见的重新引用并不总是按预期工作,例如在这种情况下.

您可以通过调整引用来解决问题:

You can solve the problem by tweaking your quoting:

... PUBLICPROPERTY=`"`" # `-escape the " chars. ... 'PUBLICPROPERTY=""' # use enclosing '...', so " chars. can be used as-is

请注意,如果您想在参数中包含 PowerShell 变量/表达式的值,则使用 '...' 将不起作用.

Note that using '...' won't work if you want to include the values of PowerShell variables / expressions in the argument.

此外,在 PSv3+ 中,您可以使用 --%,停止解析符号,使 PowerShell 按原样传递剩余参数,就好像您从 cmd.exe/一个批处理文件(包括%OS%等环境变量引用的扩展).

Additionally, in PSv3+ you can use --%, the stop-parsing symbol, to make PowerShell pass the remaining arguments through as-is, as if you had called from cmd.exe / a batch file (including expansion of environment-variable references such as %OS%).

... --% PUBLICPROPERTY=""

同样,您将无法以这种方式在参数中引用 PowerShell 变量或表达式.

Again, you won't be able to reference PowerShell variables or expressions in the arguments that way.

至于没有上述技术会发生什么:

As for what happens without the techniques above:

  • PUBLICPROPERTY="someValue" 变成PUBLICPROPERTY=someValue

PUBLICPROPERTY="some Value",由于空格,变成"PUBLICPROPERTY=some Value",即整个参数都包含在"..."中.

PUBLICPROPERTY="some Value", due to whitespace, becomes "PUBLICPROPERTY=some Value", i.e., the entire argument is enclosed in "...".

PowerShell-在内部诸如PUBLICPROPERTY="someValue"之类的参数的引号被去除:如果您将这样的参数传递给PowerShellcmdlet 或函数,它只会看到 PUBLICPROPERTY=someValue.

PowerShell-internally an argument such as PUBLICPROPERTY="someValue" has its quotes stripped: if you pass such an argument to a PowerShell cmdlet or function, it will see just PUBLICPROPERTY=someValue.

在将这样的值传递给外部程序时,PowerShell 会根据情况决定是否需要双引号,但该引用只会应用于整个参数 - " 字符的初始位置丢失.

On passing such a value on to an external program, PowerShell decides situationally whether double-quoting is needed, but that quoting is then only ever applied to the entire argument - the initial placement of the " chars. is lost.

因此,PUBLICPROPERTY="someValue" 变成了 PUBLICPROPERTY=someValue 并按原样传递,因为它包含没有嵌入空格,因此 PowerShell 不应用双引号.

Thus, PUBLICPROPERTY="someValue" turns into PUBLICPROPERTY=someValue and is passed on as-is, because it contains no embedded whitespace, so PowerShell applies no double-quoting.

相比之下,PUBLICPROPERTY="some Value" 变成了 PUBLICPROPERTY=some Value,作为 "PUBLICPROPERTY=some Value" 传递>,因为空格的存在需要双引号才能将值保留为单个参数.

By contrast, PUBLICPROPERTY="some Value" turns into PUBLICPROPERTY=some Value, which is passed on as "PUBLICPROPERTY=some Value", because the presence of whitespace requires double-quoting in order to preserve the value as a single argument.

请注意,PowerShell 只对传递给外部程序的参数应用双-引用,因为这是唯一可以假定所有人都能理解的引用样式程序.

Note that PowerShell only ever applies double-quoting to arguments passed to external programs, because that is the only style of quoting that can be assumed to be understood by all programs.

重新引用逻辑随着时间的推移发生了变化,并且具有令人遗憾的是,由于向后兼容性问题,这些错误仍然存​​在.

The re-quoting logic has changed over time and has bugs, which, regrettably, are here to stay due to backward compatibility concerns.

例如,'3 " of rain' 变成了 "3 " of rain",它被破坏,因为嵌入" 缺少转义;解决方法是预测并明确执行 PowerShell 应该自动执行的操作:将嵌入的 " 转义为\" 为了外部程序的利益:'3 \" of rain'

E.g, '3 " of rain' becomes "3 " of rain", which is broken, because the embedded " lacks escaping; the workaround is to anticipate that and explicitly do what PowerShell should be doing automatically: escape the embedded " as \" for the benefit of the external program: '3 \" of rain'

更多推荐

在命令行上设置公共属性值

本文发布于:2023-11-14 00:12:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!