13

I'm trying to run MSTest.exe from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE. What's more, I'm taking all of the assemblies in my current directory and setting them as separate /testcontainer arguments. I cannot figure out how to do this without PowerShell complaining.

$CurrentDirectory = [IO.Directory]::GetCurrentDirectory()

$MSTestCall = '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"'

foreach($file in Get-ChildItem $CurrentDirectory) 
{
    if($file.name -match "\S+test\S?.dll$" )
    {
        $MSTestArguments += "/TestContainer:" + $file + " "
    }
}

$MSTestArguments += " /resultsFile:out.trx"
$MSTestArguments += " /testsettings:C:\someDirectory\local64.testsettings"

Invoke-Expression "$MSTestCall $MSTestArguments"

The error I get from this code is:

Invoke-Expression : You must provide a value expression on the right-hand side of the '/' operator.

I don't get this error when I try to call a mstest.exe in a directory without a space in the name (no additional "'s are needed).

When I try using &,

&$MSTestCall $MSTestArguments

It hands $MSTestArguments over as a single argument, which MSTest prompty throws out. Suggestions?

2
  • 1
    The extra quotes here are unnecessary (and in fact cause problems in this case) - $MSTestCall = '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"'. Once you put "foo bar.exe" into a variable $foo then it contains that string complete with the space. Calling & $foo works as expected i.e it executes the command named by the string in variable $foo. Commented Oct 6, 2010 at 6:07
  • 2
    One other note about strings and regexes. Generally, unless I need to specify a PowerShell variable within the regex I use single quoted strings so PowerShell doesn't "interpret" things like $1. Also you specify .dll and I suspect you want \.dll. The whole thing with single quotes - '\S+test\S?\.dll$'. Commented Oct 6, 2010 at 6:12

2 Answers 2

24

I would recommend you to use an array of parameters and the operator &. See the examples in my answer in here: Executing a Command stored in a Variable from Powershell

In this case the code should be something like this:

$MSTestCall = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"
$MSTestArguments = @('/resultsFile:out.trx', '/testsettings:C:\someDirectory\local64.testsettings')

foreach($file in Get-ChildItem $CurrentDirectory)  
{ 
    if($file.name -match "\S+test\S?.dll$" ) 
    { 
        $MSTestArguments += "/TestContainer:" + $file
    } 
} 

& $MSTestCall $MSTestArguments
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. This is a great solution. Thank you.
-1

Does this work?

$MSTestCall = @'"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"'@

1 Comment

That won't work because you have to start a newline after the opening here string sequence @'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.