如何逃避PowerShell保留字?

时间:2015-10-12 13:07:35

标签: powershell powershell-v2.0

我正在使用PowerShell编写一个命令来执行OpenCover,其中包含一个标记-filter

然而事实证明Filter是PowerShell中的保留字之一。

我生成的命令(在CMD下运行正常)是:

OpenCover.Console.exe -output:Coverage.xml -register:user -filter:"+[*]* -[*]*.Test*" -target:nunit-console.exe -targetargs:"Test.dll /config:Release /noshadow /nologo /labels"

但是当我在PowerShell中运行它时出现错误:

Incorrect Arguments: The argument [*]*.Test* is not recognised

2 个答案:

答案 0 :(得分:3)

这与PowerShell中保留字filter无关。 PowerShell如何将参数传递给本机应用程序的问题。您的命令行将作为:

传递
OpenCover.Console.exe -output:Coverage.xml -register:user "-filter:"+[*]* -[*]*.Test*"" -target:nunit-console.exe "-targetargs:"Test.dll /config:Release /noshadow /nologo /labels""

PowerShell添加的一些额外引号会导致错误的命令行传递给本机应用程序。

您可以尝试的第一件事是将命令行更改为以下内容:

OpenCover.Console.exe -output:Coverage.xml -register:user "-filter:+[*]* -[*]*.Test*" -target:nunit-console.exe "-targetargs:Test.dll /config:Release /noshadow /nologo /labels"

在这种情况下,PowerShell不会添加额外的引号,并且OpenCover.Console.exe可以识别您的命令。

如果这没有帮助,那么你可以使用Start-Process cmdlet,它永远不会添加任何额外的qoutes:

Start-Process OpenCover.Console.exe '-output:Coverage.xml -register:user -filter:"+[*]* -[*]*.Test*" -target:nunit-console.exe -targetargs:"Test.dll /config:Release /noshadow /nologo /labels"' -NoNewWindow -Wait

答案 1 :(得分:0)

您是否尝试过阅读PowerShell technet上的“about_Escape_Characters”部分? https://technet.microsoft.com/en-us/library/hh847755.aspx

STOP-PARSING SYMBOL

调用其他程序时,可以使用停止解析符号( - %)来阻止Windows PowerShell生成错误或错误解释程序参数。停止解析符号是在程序调用中使用转义字符的替代方法。它是在Windows PowerShell 3.0中引入的。

例如,以下命令在Icacls命令中使用停止解析符号:

icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F

有关停止解析符号的更多信息,请参阅about_Parsing。

相关问题