简体   繁体   English

从PowerShell运行cmd.exe

[英]Run cmd.exe from PowerShell

I am trying to run some unit tests using PowerShell. 我正在尝试使用PowerShell运行一些单元测试。 I have a command that (sort of) works: 我有一个命令(有点)工作:

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

The problem with this is that the standard out and standard error streams don't come out in the right order - they get mixed up. 这样做的问题是标准输出和标准错误流没有以正确的顺序出现 - 它们混淆了。 One solution to this (from here ) is to use cmd.exe, but I cannot get this to work. 对此的一个解决方案(从这里 )是使用cmd.exe,但我不能让它工作。 I feel like I've tried everything I can think of. 我觉得我已经尝试了所有我能想到的东西。

Here's what I have: 这就是我所拥有的:

$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

This last line does not work. 最后一行不起作用。 Wierdly, if I just have 很奇怪,如果我有

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

Then this runs, but of course it's no use to me. 然后就跑了,但当然对我没用。 It's the second parameter that's the problem. 这是问题的第二个参数。 Other things I've tried. 我试过的其他事情。 How can I get this to run? 我怎么能让它运行?

$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

When you run this PowerShell command, 运行此PowerShell命令时

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

PowerShell produces this command line: PowerShell生成此命令行:

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

PowerShell encloses values of $vsTestPath and $TestAssembly in quotes as they contain spaces and the first character is not a quote itself. PowerShell将$vsTestPath$TestAssembly$TestAssembly在引号中,因为它们包含空格,并且第一个字符本身不是引号。 Now you have to understand how cmd processes this command line (see cmd /? ): 现在您必须了解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.

As you can see, we have more than two quotes and first character is quote, so cmd will strip first and last quote from the command line: 如您所见,我们有两个以上的引号,第一个字符是引号,因此cmd将从命令行中删除第一个和最后一个引号:

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

Now you actually starting C:\\Program with some arguments, and you very likely does not have C:\\Program.exe or C:\\Program.cmd . 现在你实际上用一些参数启动C:\\Program ,你很可能没有C:\\Program.exeC:\\Program.cmd Solution: add extra quotes to make cmd happy: 解决方案:添加额外的引号以使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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM