如何避开PowerShell保留字?

如何避开PowerShell保留字?,powershell,powershell-2.0,Powershell,Powershell 2.0,我正在使用PowerShell编写一个命令来执行带有标志的OpenCover 但事实证明,这是PowerShell中的保留字之一 我生成的命令(在CMD下运行良好)是: 但当我在PowerShell中运行它时,我得到一个错误: Incorrect Arguments: The argument [*]*.Test* is not recognised 您是否尝试过阅读PowerShell technet上的“关于转义字符”部分 停止解析符号 调用其他程序时,可以使用停止解析符号(--%)来防止

我正在使用PowerShell编写一个命令来执行带有标志的
OpenCover

但事实证明,这是PowerShell中的保留字之一

我生成的命令(在
CMD
下运行良好)是:

但当我在PowerShell中运行它时,我得到一个错误:

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

您是否尝试过阅读PowerShell technet上的“关于转义字符”部分

停止解析符号

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

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

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

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

这与在PowerShell中作为保留字的
过滤器无关。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,它永远不会添加任何额外的QOUTE:

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

嗨,克里斯,我不能一字不差地使用
因为我在PowerShell 2.0上太棒了<代码>启动流程
工作起来很有魅力!
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