从PowerShell中运行cmd.exe

6

我正在尝试使用PowerShell运行一些单元测试。我有一个(有点)能用的命令:

$output = & $vsTestPath $TestAssembly $logger $TestCaseFilter 2>&1

这种方法存在一个问题,标准输出和标准错误流的输出顺序不正确,它们会混合在一起。从这里得到的一个解决方案是使用cmd.exe,但我无法让它工作。我感觉自己已经尝试了所有能想到的方法。
以下是我所拥有的内容:
$vsTestPath = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
$TestAssembly = "C:\IntegrationTesting\Test Application\Ads.Slms.IntegrationTesting.Web.Smartfill.dll"
$output = & cmd.exe /c $vsTestPath $TestAssembly 2`>`&1

这最后一行不起作用。奇怪的是,如果我只写
$output = & cmd.exe /c $vsTestPath 2`>`&1

然后这个运行了,但是对我没有用。问题在于第二个参数。我尝试了其他的东西。我该如何让它运行呢?

$output = & cmd.exe /c $vsTestPath $TestAssembly 2`>`&1
$output = & cmd.exe /c $vsTestPath,$TestAssembly 2`>`&1
$output = & cmd.exe /c "$vsTestPath" $TestAssembly 2`>`&1
$output = & cmd.exe /c """$vsTestPath""" $TestAssembly 2`>`&1
$output = & cmd.exe /c "$vsTestPath" "$TestAssembly" 2`>`&1
$output = & cmd.exe /c """$vsTestPath""" """$TestAssembly""" 2`>`&1

尝试这个:$output = & cmd.exe /c '"' $vsTestPath $TestAssembly '2>&1' '"' - user4003407
如果您查看上面链接中引用的示例,您会发现该示例不包括'&',但我过去曾使用过$output = cmd.exe /c $vsTestPath $TestAssembly 2>&1 - mhatch73
@PetSerAl - 这个方法有效!为什么有效,我不太确定。看起来有点像黑魔法 :-) 如果您将其作为答案添加,我可以接受它。感谢所有的回答 - 我无法告诉您我有多感激这个工作。 mhatch73 - 很遗憾,这个方法没有起作用('C:\Program' 不被认为是内部或外部命令)。我应该指出,在 vsTestPath 和 TestAssembly 中有空格 - 我想这就是导致问题的原因。 - bornfromanegg
1个回答

13

当您运行此 PowerShell 命令时,

& cmd.exe /c $vsTestPath $TestAssembly 2`>`&1

PowerShell 生成此命令行:

cmd.exe /c "C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll" 2>&1

由于$vsTestPath$TestAssembly包含空格且第一个字符本身不是引号,因此PowerShell在引号中封闭这些值。现在您必须了解cmd如何处理此命令行(请参见cmd /?):

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

正如您所看到的,我们有超过两个引号并且第一个字符是引号,因此cmd将从命令行中剥离第一个和最后一个引号:

C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll 2>&1

现在您正在使用一些参数实际启动C:\Program,很可能没有C:\Program.exeC:\Program.cmd。解决方案:添加额外的引号以使cmd正常运作:

& cmd.exe /c `" $vsTestPath $TestAssembly 2`>`&1 `"
cmd.exe /c " "C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll" 2>&1 "
"C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll" 2>&1

很好的答案,解释得也非常好。谢谢。 - bornfromanegg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接