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

标签 powershell spaces

我正在尝试从 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE 运行 MSTest.exe .更重要的是,我将获取当前目录中的所有程序集并将它们设置为单独的/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"

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

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



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

当我尝试使用 & ,
&$MSTestCall $MSTestArguments

它将 $MSTestArguments 作为单个参数传递,MSTest 立即抛出。建议?

最佳答案

我建议您使用参数数组和运算符 & .在此处查看我的回答中的示例:Executing a Command stored in a Variable from 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

关于powershell - 使用 PowerShell 从包含空格的目录运行 EXE 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3868342/

相关文章:

html - 如何在我们的 HTML 中的每个句点后放置两个空格?

powershell v2 函数,如何在不命名第二个参数的情况下为第一个参数设置默认值?

powershell - 如何在不下载到本地驱动器的情况下运行位于公共(public) Git 文件夹中的 powershell 脚本

c - 如何在 C 中打印二维字符串数组(带空格)?

emacs:想要缩进制表符(制表符大小 2 或 4)与空格对齐

regex - Perl Regexp::Common 意外匹配错误的 URL

function - 在Powershell中使用哈希表格式化属性

python - Tcl 在我的 Python27 目录中似乎位于错误的位置。我怎样才能改变这个?

powershell - 如何评估 PowerShell 中是否返回数据或错误?