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

发布于 2024-09-26 09:57:44 字数 1018 浏览 9 评论 0原文

我正在尝试从 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:您必须在“/”运算符的右侧提供一个值表达式。

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

当我尝试使用 & 时,

&$MSTestCall $MSTestArguments

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-10-03 09:57:44

我建议您使用参数数组和运算符&。请参阅我的答案中的示例: 执行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

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
七秒鱼° 2024-10-03 09:57:44

这有效吗?

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

Does this work?

$MSTestCall = @'"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"'@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文