这个 PowerShell 命令行引用/转义是怎么回事?
我显然不知道自己在做什么。
我终于让这个 PowerShell 命令可以工作了。但我不明白为什么它有效。
我关心的是最后的 "" 字符:
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="$build_directory\deploy" `
-dest:contentPath="$server_temp_directory,computerName=$server,username=$server_username,password=$server_password" `
-verbose `
-postSync=runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""
为什么我需要双双引号?
我的 IDE (PowerGUI) 表示该行未正确结束,但这是我可以使命令按需要运行的唯一方法。
我和 IDE 不知道 PowerShell 中的引用是什么?
来自 echoargs 的一点输出:
如果我运行:
echoargs -postSync=runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""
我得到:
Arg 0 is <-postSync=runCommand=powershell -NoLogo -NoProfile -Command \remotetasks.ps1 Deploy>
如果我在没有双双引号的情况下运行,我得到:
Arg 0 is <-postSync=runCommand=powershell>
Arg 1 is <-NoLogo>
Arg 2 is <-NoProfile>
Arg 3 is <-Command>
Arg 4 is <\remotetasks.ps1>
Arg 5 is <Deploy>
另一件需要注意的是,上面的命令只有在使用 = 时才有效最后一个参数中的 :
。
这不起作用:
-postSync:runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""
我已经尝试过这样的数组解决方案:
$arguments = @("-verb:sync",
"-source:contentPath=$build_directory\deploy",
"-dest:contentPath=$server_temp_directory,computerName=$server,username=$server_username,password=$server_password",
"-verbose",
"-postSyncOnSuccess:runCommand=powershell -Command $server_temp_directory\remotetasks.ps1 Deploy")
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" $arguments
我仍然收到相同的错误:
错误:无法识别的参数 '"-postSyncOnSuccess:runCommand=powershell -Command c:\temp\kslog\remotetasks.ps1 Deploy"'。所有参数必须以“-”开头。
我在这里做错了什么新事情吗?
I obviously don't know what I'm doing.
I have finally got this PowerShell command to work. But I can't figure out why it works.
My concern is the final "" characters:
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="$build_directory\deploy" `
-dest:contentPath="$server_temp_directory,computerName=$server,username=$server_username,password=$server_password" `
-verbose `
-postSync=runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""
Why do I need double double-quotes?
My IDE (PowerGUI) says the line is not ended correctly, but it is the only way I can make the command run as wanted.
What is it, that I - and the IDE - don't know about quoting in PowerShell?
A little output from echoargs:
If I run:
echoargs -postSync=runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""
I get:
Arg 0 is <-postSync=runCommand=powershell -NoLogo -NoProfile -Command \remotetasks.ps1 Deploy>
If I run without the double double-quotes, I get:
Arg 0 is <-postSync=runCommand=powershell>
Arg 1 is <-NoLogo>
Arg 2 is <-NoProfile>
Arg 3 is <-Command>
Arg 4 is <\remotetasks.ps1>
Arg 5 is <Deploy>
Another thing to notice is that the above command does only work if it uses = instead of :
in the last argument.
This won't work:
-postSync:runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""
I have tried the array solution like this:
$arguments = @("-verb:sync",
"-source:contentPath=$build_directory\deploy",
"-dest:contentPath=$server_temp_directory,computerName=$server,username=$server_username,password=$server_password",
"-verbose",
"-postSyncOnSuccess:runCommand=powershell -Command $server_temp_directory\remotetasks.ps1 Deploy")
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" $arguments
I still get the same error:
Error: Unrecognized argument '"-postSyncOnSuccess:runCommand=powershell -Command c:\temp\kslog\remotetasks.ps1 Deploy"'. All arguments must begin with "-".
Am I doing some new thing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个臭名昭著的问题。 “执行需要引号和变量的命令实际上是不可能的”票证是投票最多的错误:https:// connect.microsoft.com/PowerShell/Feedback
您还可以找到一些解决方法。但我建议您将所有参数组成一个数组,并使用
&
运算符通过该数组调用本机命令。请参阅答案和示例:运行 EXE 文件从包含空格的目录使用 PowerShell
和
从 Powershell 执行存储在变量中的命令< /a>
我没有使用
msdeploy.exe
,因此无法为您的案例提供一些演示代码。但我将这种方法很好地应用到了许多其他棘手的本机命令中。请尝试一下并让我们知道结果。PS 从技术上讲,这并不完全是您问题的答案,但我认为您仍在寻找一种实用的方法来做到这一点,所以它仍然可能会有所帮助。
This is a notorious issue. The ticket “Executing commands which require quotes and variables is practically impossible” is the most voted bug: https://connect.microsoft.com/PowerShell/Feedback
You can find there a few workarounds as well. But I would recommend you to compose all the parameters as an array and use the
&
operator to invoke a native command with this array. See the answers and examples:Running an EXE file using PowerShell from a directory with spaces in it
and
Executing a Command stored in a Variable from Powershell
I did not work with
msdeploy.exe
and cannot provide some demo code for your case. But I applied this approach to many other tricky native commands well enough. Please, try it and let us know the results.P.S. Technically this is not exactly an answer to your questions but I assume you are still looking for a practical way of doing this, so it still might be helpful.
我终于知道如何做到这一点。这是一个示例脚本:
I finally found out how to do this. Here is a sample script:
这里的错误区域之一是 PowerShell 无法处理基于 cmd 的 msdeploy 中表达的“程序文件”之间的空格转换。
这是一个非常基本的解决方案,并利用了过时的 DOS 限制,但可以使用以下非空白引用来代替:
它很蹩脚,但它有效。
您还可以创建一个调用 cmd 并执行命令的函数。此示例还假设您尝试调用的内容(msdeploy 等)位于路径中。
One of the areas at fault here is the inability of PowerShell to cope with the translation of the whitespace between "Program Files" expressed in the cmd-based msdeploy.
This is an extremely basic solution and leverages antiquated DOS limitations, but the following non-whitespace references can be used in their stead:
It's lame, but it works.
You can also create a function that calls cmd and executes your command. This example also assumes what you're trying to call (msdeploy, etc.) is in the path.