Running an EXE file using PowerShell from a directory with spaces in it

▼魔方 西西 提交于 2019-12-29 04:42:06

问题


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?


回答1:


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



回答2:


Does this work?

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


来源:https://stackoverflow.com/questions/3868342/running-an-exe-file-using-powershell-from-a-directory-with-spaces-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!