Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run cmd.exe from PowerShell

Tags:

powershell

cmd

I am trying to run some unit tests using 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. 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
like image 786
bornfromanegg Avatar asked Feb 18 '15 09:02

bornfromanegg


People also ask

Can I run cmd commands in PowerShell?

The executables can be run from any command-line shell, like PowerShell. This includes script files that may require other shells to work properly. For example, if you run a Windows batch script ( . cmd file) in PowerShell, PowerShell runs cmd.exe and passes in the batch file for execution.

Is cmd.exe the same as PowerShell?

CMD is the command line for Microsoft Windows operating system, with command-based features. Powershell is a task-based command-line interface, specifically designed for system admins and is based on the . Net Framework.

How do I run an exe as administrator in PowerShell?

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key. Step 2: Now, the command prompt will turn to Windows PowerShell. Step 3: Type the command start-process PowerShell -verb runas and press "enter" key. Step 4: It will bring up an elevated Windows PowerShell as an administrator.


1 Answers

When you run this PowerShell command,

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

PowerShell produces this command line:

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. Now you have to understand how cmd processes this command line (see 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:

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. Solution: add extra quotes to make cmd happy:

& 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
like image 114
user4003407 Avatar answered Sep 30 '22 00:09

user4003407