gpt4 book ai didi

powershell - 从 PowerShell 运行 cmd.exe

转载 作者:行者123 更新时间:2023-12-04 22:45:32 25 4
gpt4 key购买 nike

我正在尝试使用 PowerShell 运行一些单元测试。我有一个(有点)有效的命令:

$output = & $vsTestPath $TestAssembly $logger $TestCaseFilter 2>&1

问题在于标准输出和标准错误流没有以正确的顺序出现 - 它们混淆了。对此的一种解决方案(来自 here )是使用 cmd.exe,但我无法让它工作。我觉得我已经尝试了我能想到的一切。

这是我所拥有的:
$vsTestPath = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
$TestAssembly = "C:\IntegrationTesting\Test Application\Ads.Slms.IntegrationTesting.Web.Smartfill.dll"
$output = & cmd.exe /c $vsTestPath $TestAssembly 2`>`&1

最后一行不起作用。奇怪的是,如果我有
$output = & cmd.exe /c $vsTestPath 2`>`&1

然后这运行,但当然它对我没有用。这是第二个参数的问题。我尝试过的其他事情。我怎样才能让它运行?
$output = & cmd.exe /c $vsTestPath $TestAssembly 2`>`&1
$output = & cmd.exe /c $vsTestPath,$TestAssembly 2`>`&1
$output = & cmd.exe /c "$vsTestPath" $TestAssembly 2`>`&1
$output = & cmd.exe /c """$vsTestPath""" $TestAssembly 2`>`&1
$output = & cmd.exe /c "$vsTestPath" "$TestAssembly" 2`>`&1
$output = & cmd.exe /c """$vsTestPath""" """$TestAssembly""" 2`>`&1

最佳答案

当您运行此 PowerShell 命令时,

& cmd.exe /c $vsTestPath $TestAssembly 2`>`&1

PowerShell 生成此命令行:
cmd.exe /c "C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll" 2>&1

PowerShell 包含 $vsTestPath 的值和 $TestAssembly在引号中,因为它们包含空格,并且第一个字符本身不是引号。现在您必须了解如何 cmd处理这个命令行(见 cmd /?):
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

1. If all of the following conditions are met, then quote characters
on the command line are preserved:

- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.

2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.

正如你所看到的,我们有两个以上的引号,第一个字符是引号,所以 cmd将从命令行中删除第一个和最后一个引号:
C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll 2>&1

现在你真的开始 C:\Program有一些论据,你很可能没有 C:\Program.exeC:\Program.cmd .解决方案:添加额外的引号使 cmd快乐的:
& cmd.exe /c `" $vsTestPath $TestAssembly 2`>`&1 `"
cmd.exe /c " "C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll" 2>&1 "
"C:\Program Files (x86)\BlaBlaBla.exe" "C:\IntegrationTesting\Test Application\BlaBlaBla.dll" 2>&1

关于powershell - 从 PowerShell 运行 cmd.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28580041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com