PowerShell脚本:从文件夹列表中排除访问被拒绝的文件夹

7

我试图编写一个脚本来列出特定路径下的文件夹和子文件夹以及文件数量。如何在脚本中排除访问被拒绝的文件夹?

我使用了 get-childitem 代码片段以及 where {$_.permission -match "read",但我不知道我尝试的是正确的还是错误的。最终出现以下错误信息:

CategoryInfo : ReadError: (\support....-242\New folder:String) [Get-ChildItem], IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

1个回答

10

您可以按cmdlet设置错误操作偏好,例如:

Get-ChildItem -recurse -ErrorAction SilentlyContinue | ? {$_.permission -match "read"}

您也可以使用系统变量在每个脚本中进行设置:

$ErrorActionPreference = "SilentlyContinue"

Get-Help about_CommonParameters


谢谢回复,我真的很喜欢这篇文章,它帮助我解决了我的问题。非常感谢。 - user2353132
我继续进行了以下操作: "get-childitem -recurse -ErrorAction SilentlyContinue | ? {$_.permission -match "read"} - user2353132

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接