使用PowerShell从包含空格的目录运行EXE文件

use*_*384 12 powershell spaces

我正在尝试运行MSTest.exe C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE.更重要的是,我将当前目录中的所有程序集都设置为单独的/ testcontainer参数.如果没有PowerShell的抱怨,我无法弄清楚如何做到这一点.

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

我从这段代码得到的错误是:

Invoke-Expression:您必须在'/'运算符的右侧提供值表达式.

当我尝试在名称中没有空格的目录中调用mstest.exe时,我不会收到此错误(不需要额外的").

当我尝试使用时&,

&$MSTestCall $MSTestArguments
Run Code Online (Sandbox Code Playgroud)

它将$ MSTestArguments作为单个参数交给MSTest提示抛出.建议?

Rom*_*min 23

我建议你使用一系列参数和运算符&.请参阅我在此处的答案中的示例:执行存储在Powershell变量中的命令

在这种情况下,代码应该是这样的:

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