powershell中的双引号问题

发布于 2025-01-20 02:15:51 字数 242 浏览 0 评论 0原文

我试图将安装参数传递给 powershell 中的变量,但这样做时出现错误。

$InstallString = "$InstallLocation\application.exe" /install / quiet CID="BsDdfi3kj" Tag="CinarCorp"

我尝试将其运行为输入“&”符号但它不起作用,我检查了很多网站但无法解决它。任何帮助将不胜感激。

谢谢..

I am trying to pass installation arguments to a variable in powershell but I get errors doing that.

$InstallString = "$InstallLocation\application.exe" /install / quiet CID="BsDdfi3kj" Tag="CinarCorp"

I tried to run this as putting "&" symbol but it didn't work and I checked many websites but I couldn't solve it. Any help will be appreciated.

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2025-01-27 02:15:51

= 右侧使用的语法仅在直接调用如下命令时才有效:

& "$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"

请注意,在 quiet 之前有一个虚假的空格字符,我已将其删除。

当您实际上想要将命令存储在变量中时,请像这样更改语法:

$InstallString = "`"$InstallLocation\application.exe`" /install /quiet CID=`"BsDdfi3kj`" Tag=`"CinarCorp`""

我已将整个字符串括在双引号内,并通过在内部双引号前面放置反引号来转义内部双引号。

您还可以使用 here-string 以避免转义内部双引号:

$InstallString = @"
"$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"
"@

请注意,实际字符串以及最终的"@ 必须从线。如果您缩进实际的字符串,则变量中会包含空格/制表符,这通常是不需要的

修剪 如果您坚持缩进:

$InstallString = @"
    "$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"
"@.Trim()

我建议阅读 关于引用规则了解更多详细信息。

The syntax used to the right of = only works when directly calling the command like this:

& "$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"

Note that you had a spurious space character before quiet which I removed.

Change the syntax like this when you actually want to store the command in a variable:

$InstallString = "`"$InstallLocation\application.exe`" /install /quiet CID=`"BsDdfi3kj`" Tag=`"CinarCorp`""

I have enclosed the whole string within double-quotes and escaped the inner double-quotes by placing a backtick in front of them.

You could also use a here-string to avoid having to escape the inner double-quotes:

$InstallString = @"
"$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"
"@

Note that the actual string as well as the final "@ have to start at the beginning of the line. If you indent the actual string, the spaces/tabs are included in the variable, which is usually not wanted.

You could of course trim the string if you insist on indentation:

$InstallString = @"
    "$InstallLocation\application.exe" /install /quiet CID="BsDdfi3kj" Tag="CinarCorp"
"@.Trim()

I recommend to read about Quoting Rules for further details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文