从PowerShell运行cmd.exe

bor*_*egg 4 powershell cmd

我正在尝试使用PowerShell运行一些单元测试.我有一个命令(有点)工作:

$output = & $vsTestPath $TestAssembly $logger $TestCaseFilter 2>&1
Run Code Online (Sandbox Code Playgroud)

这样做的问题是标准输出和标准错误流没有以正确的顺序出现 - 它们混淆了.对此的一个解决方案(从这里)是使用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
Run Code Online (Sandbox Code Playgroud)

最后一行不起作用.很奇怪,如果我有

$output = & cmd.exe /c $vsTestPath 2`>`&1
Run Code Online (Sandbox Code Playgroud)

然后就跑了,但当然对我没用.这是问题的第二个参数.我试过的其他事情.我怎么能让它运行?

$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
Run Code Online (Sandbox Code Playgroud)

use*_*407 10

运行此PowerShell命令时

& cmd.exe /c $vsTestPath $TestAssembly 2`>`&1
Run Code Online (Sandbox Code Playgroud)

PowerShell生成此命令行:

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

PowerShell包含值$vsTestPath$TestAssembly引号的值,因为它们包含空格,第一个字符本身不是引号.现在您必须了解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.
Run Code Online (Sandbox Code Playgroud)

如您所见,我们有两个以上的引号,第一个字符是引号,因此cmd将从命令行中删除第一个和最后一个引号:

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

现在你实际上从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
Run Code Online (Sandbox Code Playgroud)