代码之家  ›  专栏  ›  技术社区  ›  asgerhallas

这个PowerShell命令行引用/转义有什么问题?

  •  12
  • asgerhallas  · 技术社区  · 14 年前

    我显然不知道我在做什么。

    我终于得到了这个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)说这行代码没有正确结束,但这是我可以让命令按需运行的唯一方法。


    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
    

    我还是犯了同样的错误:

    我在这里做错什么了吗?

    3 回复  |  直到 5 年前
        1
  •  11
  •   Community VonC    7 年前

    这是一个臭名昭著的问题。票证执行命令需要引号和变量实际上是不可能的,这是投票最多的错误: https://connect.microsoft.com/PowerShell/Feedback

    你也可以找到一些解决办法。但我建议你把所有的参数 使用 & Running an EXE file using PowerShell from a directory with spaces in it Executing a Command stored in a Variable from Powershell

    我没有和 msdeploy.exe

    附笔。从技术上讲,这并不能完全回答你的问题,但我认为你仍在寻找一种切实可行的方法来完成这项工作,因此这可能还是有帮助的。

        2
  •  3
  •   Peter Mortensen Len Greski    5 年前

    我终于知道怎么做了。下面是一个示例脚本:

    $src = 'C:\sandbox\Test Folder\A'
    $dest = 'C:\sandbox\Test Folder\B'
    $msdeploy = Get-Command 'C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe'
    $command = "& `$msdeploy --% -verb:sync -source:contentPath=""$src"" -dest:contentPath=""$dest"""
    $sb = $ExecutionContext.InvokeCommand.NewScriptBlock($command)
    & $sb
    
        3
  •  1
  •   Peter Mortensen Len Greski    5 年前

    这里的一个错误是PowerShell无法处理基于cmd的msdeploy中表示的“程序文件”之间的空格转换。

    这是一个非常基本的解决方案,利用了过时的DOS限制,但可以使用以下非空白引用来代替它们:

    \Program Files\ ... \Progra~1\
    \Program Files (x86)\ ... \Progra~2\
    

    这很蹩脚,但很管用。

    function PushToTarget() {
         cmd.exe /C $("msdeploy.exe -verb:sync -source:apphostconfig=yourwebapp-dev -dest:archivedir=d:\backup")
    }