用于从文件夹列表中省略遍历访问被拒绝的文件夹的 Powershell 脚本

标签 powershell powershell-2.0 powershell-3.0

我试图做一个脚本来列出文件夹和子文件夹的提取以及特定目录路径的文件数。如何排除脚本中拒绝访问的文件夹?

我用了 get-childitem代码片段以及 where {$_.permission -match "read" ,我不知道我的尝试是否正确。我结束了以下错误:消息:

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

最佳答案

您可以为每个 cmdlet 设置错误操作首选项,例如:

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

或者您可以使用 System 变量为每个脚本设置它:
$ErrorActionPreference = "SilentlyContinue"Get-Help about_CommonParameters

关于用于从文件夹列表中省略遍历访问被拒绝的文件夹的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23119010/

相关文章:

powershell - 有什么方法可以告诉以 start-job -filename 启动时正在运行的脚本是什么?

powershell - 使用PowerShell更改AD中电话号码的格式

csv - 重建 CSV 并使用字段值作为导出的文件名

Powershell:压缩文件

powershell - 将文档添加到自定义方法 (ScriptMethod)

Powershell Write-EventLog/Get-WinEvent 消息问题

rest - 使用 Powershell 将文件上传到 Box.com

file - Powershell:复制子文件夹中的选定文件

powershell - 以编程方式创建参数 block 时无法生成 ParameterSetMetadata

powershell - 改进代码以在 PowerShell 中使用可选开关参数