选项-递归有时不适用于PowerShell cmdlet get-childitem

选项-递归有时不适用于PowerShell cmdlet get-childitem,powershell,Powershell,假设我有这个文件: C:\[foo]\bar 然后运行以下PowerShell命令: $path = 'C:\[foo]' get-childitem -literalpath $path 一切正常,get childitem显示文件bar,然后立即返回 现在我添加了-recurse选项: $path = 'C:\[foo]' get-childitem -literalpath $path -recurse get childitem不再显示文件bar。此外,cmdlet运行很长时间,并

假设我有这个文件:

C:\[foo]\bar
然后运行以下PowerShell命令:

$path = 'C:\[foo]'
get-childitem -literalpath $path
一切正常,
get childitem
显示文件
bar
,然后立即返回

现在我添加了
-recurse
选项:

$path = 'C:\[foo]'
get-childitem -literalpath $path -recurse
get childitem
不再显示文件
bar
。此外,cmdlet运行很长时间,并为
C:\Windows
下的文件夹显示各种“权限被拒绝”错误消息,这显然是因为它扫描整个C:drive

问题在于文件夹
[bar]
的名称中有括号。如果我将文件夹重命名为
bar
,即不带括号,则递归结果将按预期工作

我的主要问题是:我如何说服
get childitem
递归扫描名称中有括号(或其他特殊字符)的文件夹

第二个问题:这是一个已知的bug吗

环境:Windows 8.1、PowerShell 4.0



编辑:我验证了在PowerShell 2.0(在Windows 7机器上)中,
get childitem
返回相同的结果,无论是否使用
-recurse
。在3.0版或4.0版中,该行为似乎发生了变化。

我也得到了与使用PowerShell 4.0的Windows 7 x64位上相同的结果。从外观上看,递归功能忽略了
-LiteralPath

这是我在讨论同一问题时发现的一个解决方法。那篇文章中的OP最后使用了
-Path
和双转义括号

$path = "C:\[foo]"
$escapedPath = $path.Replace("[","``[").Replace("]","``]")
Get-ChildItem -Path $escapedPath -Recurse
注意:如果删除
-Recurse

Get-ChildItem : Cannot find path 'C:\`[foo`]' because it does not exist.

虽然丑得要命,但它确实奏效了。在使用PowerShell时,我还需要注意一个技巧:-(HeZube等待它。我还没有真正回答这个问题。仍然不知道你的问题的真正原因。比我更有经验的人可能知道正在发生什么事。当然,如果有人有更好的答案,我会考虑它,但目前我要与你的解决方案。你知道,这是一个在这里失败的构建脚本,所以。有一些紧急情况:-)报告这是一个错误。如果你认为这应该被修复,请投票。