Powershell 5 Get-ChildItem LiteralPath 不再与 Include 一起使用 [英] Powershell 5 Get-ChildItem LiteralPath doesn't work with Include anymore

查看:59
本文介绍了Powershell 5 Get-ChildItem LiteralPath 不再与 Include 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用 PowerShell 5.0.10586.0 更新到 Windows 10 TH2 Build 10586

Right now I updated to Windows 10 TH2 Build 10586 with PowerShell 5.0.10586.0

现在我遇到了 Get-ChildItem 的问题

Now I got a problem with Get-ChildItem

$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Include *.txt

这将返回 $path 中的所有文件,即使它们不是 .txt.这在更新之前有效.当我把它改成

This returns ALL files in $path even they are not .txt. This was working before the update. When I change it to

$files = Get-ChildItem -Path $path -Force -Recurse -Include *.txt

它又工作了.但这不是我想要的.这是错误还是我做错了什么?

it works again. But that's not what I want. Is this a bug or am I doing something wrong?

推荐答案

就个人而言,我从不使用 -Include-Exclude 了.我总是通过 Where-Object 管道.我不知道 -Include-Exclude 的作者是否疯了,或者底层的 .Net 提供者是否有问题,但它们非常脆弱.

Personally, I never use -Include or -Exclude anymore. I always pipe through Where-Object. I don't know if the author of -Include and -Exclude was insane or if there's a problem with the underlying .Net provider, but they're flaky as hell.

我在 5.0.10240.16384.

I'm on 5.0.10240.16384.

gci -Path $path -Include *.txt -Force

什么都不返回.

gci -LiteralPath $path -Include *.txt -Force

返回 $path 中的所有内容.

Returns everything in $path.

gci -LiteralPath $path -Include *.txt -Force -Recurse
gci -Path $path -Include *.txt -Force -Recurse

都返回 $path 和所有子文件夹中的 *.txt.

Both return *.txt in $path and all subfolders.

那么正确的行为应该是什么?-Recurse 标志是否修改了 -Include 的工作方式?我不知道.我不再关心了.我不会处理这种行为.我只是用这个:

So what's the proper behavior supposed to be? Does the -Recurse flag modify how -Include works? I don't know. I no longer care. I'm not going to deal with that kind of behavior. I just use this:

gci -Path $path -Recurse -Force | Where-Object { $_.Extension -eq '.txt' }

我依靠Get-ChildItem 来枚举文件和文件夹就是这样.只要给我对象,我就会过滤它们.就像所有旧的 Remove-Item -Recurse 错误一样,有些东西并没有像人们期望的那样工作.

I rely on Get-ChildItem to enumerate files and folders and that's it. Just give me the objects and I'll filter them. Like all the old Remove-Item -Recurse bugs, there's something there that just doesn't work the way people expect it to.

这篇关于Powershell 5 Get-ChildItem LiteralPath 不再与 Include 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆