此PowerShell命令行引用/转义是怎么回事? [英] What is up with this PowerShell command line quoting/escaping?
问题描述
我显然不知道自己在做什么.
I obviously don't know what I'm doing.
我终于得到了这个PowerShell命令.但是我不知道为什么会起作用.
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""
为什么需要双引号?
我的IDE(PowerGUI)表示该行未正确结束,但这是使命令按需运行的唯一方法.
My IDE (PowerGUI) says the line is not ended correctly, but it is the only way I can make the command run as wanted.
这是什么,我-和IDE-不知道在PowerShell中引用吗?
What is it, that I - and the IDE - don't know about quoting in 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>
如果我没有双双引号,我得到:
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.
这行不通:
-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:
错误:无法识别的参数'"-postSyncOnSuccess:runCommand = powershell -Command c:\ temp \ kslog \ remotetasks.ps1 Deploy".所有参数都必须以-"开头.
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?
推荐答案
这是一个臭名昭著的问题.票证执行几乎不需要引号和变量的命令"是最受欢迎的错误: https://connect.microsoft.com/PowerShell/Feedback
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
您也可以找到一些解决方法.但是我建议您将所有参数组合为 array ,并使用&
运算符使用此数组调用本机命令.请参阅答案和示例:
运行EXE文件在其中包含空格的目录中使用PowerShell
和
从Powershell执行存储在变量中的命令
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
我没有使用msdeploy.exe
,并且无法为您的案例提供一些演示代码.但是我将这种方法很好地应用于了许多其他棘手的本机命令.请尝试一下,让我们知道结果.
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.从技术上讲,这并不完全是对您问题的答案,但我认为您仍在寻找一种实用的方法来完成此操作,因此它可能仍会有所帮助.
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.
这篇关于此PowerShell命令行引用/转义是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!