使用 PowerShell 从包含空格的目录运行 EXE 文件 [英] Running an EXE file using PowerShell from a directory with spaces in it

查看:53
本文介绍了使用 PowerShell 从包含空格的目录运行 EXE 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :您必须在/"运算符的右侧提供一个值表达式.

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

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

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).

当我尝试使用 & 时,

&$MSTestCall $MSTestArguments

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

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

推荐答案

我建议您使用 array 参数和运算符 &.在此处查看我的回答中的示例:执行来自 Powershell 的存储在变量中的命令

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

这篇关于使用 PowerShell 从包含空格的目录运行 EXE 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆