首页 > 解决方案 > 以递归方式列出域用户可以访问的所有目录

问题描述

递归列出所有目录很容易,如下所示:

Get-ChildItem G:\ -recurse -directory

编写一个列出特定目录权限的函数很容易:

get-acl $folder.access

如何递归检查每个目录的 ACL,然后输出仅包含域用户组可访问的列表?

标签: powershell

解决方案


你可以这样做 :

Get-ChildItem G:\ -Directory -Recurse -ErrorAction SilentlyContinue | 
   Where { try { (Get-Acl -LiteralPath $_.FullName | Select -ExpandProperty Access | 
      Where { $_.IdentityReference -eq "MYDOMAIN\Domain Users" `
              -and ($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Read) -eq [System.Security.AccessControl.FileSystemRights]::Read `
              -and $_.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow }) } catch { } }

里面的try catchWhere-Object是为了避免错误,因为Get-Acl当你无权访问路径时总是会抛出错误,即使你设置了-ErrorAction SilentlyContinue

的解释Where-Object

搜索“MYDOMAIN\域用户”:

$_.IdentityReference -eq "MYDOMAIN\Domain Users"

至少具有读取权限

($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Read) -eq [System.Security.AccessControl.FileSystemRights]::Read

允许类型(可能被拒绝)

$_.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow


推荐阅读