繁体   English   中英

使用 Powershell 获取 ACL 文件夹和子文件夹 + 用户

[英]Get ACL Folder & Subfolder + Users Using Powershell

是否可以获得文件夹及其子文件夹的权限,然后显示与该组关联的路径、组和用户? 所以,看起来像这样。 或者它必须一次是一个文件夹。

-Folder1

-Line separator
-Group
-Line separator
-List of users

-Folder2

 -Line separator
 -Group
 -Line separator
 -List of users

到目前为止我想出的脚本被警告我对powershell的经验很少。 (别担心我的老板知道。)

Param([string]$filePath)

$Version=$PSVersionTable.PSVersion
if ($Version.Major -lt 3) {Throw "Powershell version out of date. Please update powershell." }

Get-ChildItem $filePath -Recurse | Get-Acl | where { $_.Access | where { $_.IsInherited -eq $false } } | select -exp Access | select IdentityReference -Unique | Out-File .\Groups.txt

$Rspaces=(Get-Content .\Groups.txt) -replace 'JAC.*?\\|','' |
 Where-Object {$_ -notmatch 'BUILTIN|NT AUTHORITY|CREATOR|-----|Identity'} | ForEach-Object {$_.TrimEnd()}
$Rspaces | Out-File .\Groups.txt

$ErrorActionPreference= 'SilentlyContinue'
$Groups=Get-Content .\Groups.txt
ForEach ($Group in $Groups)
{Write-Host"";$Group;Write-Host --------------
;Get-ADGroupMember -Identity $Group -Recursive | Get-ADUser -Property DisplayName | Select Name}

这仅显示组和用户,而不显示路径。

好吧,让我们从头开始! 太好了,您实际上声明了一个参数。 您可能要考虑的是为参数设置默认值。 我要做的是使用当前目录,该目录方便地具有一个自动变量$PWD (我相信这是PowerShell Working Directory的缩写)。

Param([string]$filePath = $PWD)

现在,如果提供了路径,它将使用该路径,但是如果没有提供路径,它将仅使用当前文件夹作为默认值。

版本检查很好。 我敢肯定有更优雅的方法可以做到这一点,但是老实说我从来没有做过任何版本检查。

现在,您正在为找到的每个组和用户查询AD(经过一定的过滤,已授予)。 我建议我们跟踪组和成员,以便对每个组和成员仅查询一次。 它可能不会节省大量时间,但是如果多次使用任何组,它将节省一些时间。 因此,为此,我们将创建一个空的哈希表来跟踪组及其成员。

$ADGroups = @{}

现在开始出现不良趋势……写入文件,然后再读回这些文件。输出到文件很好,或者保存配置,或者在当前PowerShell会话之外再次需要但要写入文件的东西仅将其读回当前会话只是浪费。 相反,您应该将结果保存到变量中,或者直接使用它们。 因此,与其获取文件夹列表,不如直接将其通过管道传递到Get-Acl ,并丢失路径,我们将在文件夹上进行ForEach循环。 请注意,我添加了-Directory开关,因此它将仅查看文件夹而忽略文件。 这种情况发生在提供程序级别,因此您可以通过这种方式从Get-ChildItem获得更快的结果。

ForEach($Folder in (Get-ChildItem $filePath -Recurse -Directory)){

现在,您要输出文件夹的路径和一行。 既然我们不放弃文件夹对象,那就很容易了:

    $Folder.FullName
    '-'*$Folder.FullName.Length

接下来,我们获取当前文件夹的ACL:

    $ACLs = Get-Acl -Path $Folder.FullName

这就是事情变得复杂的地方。 我从ACL中获取组名,但是我合并了几个您的Where语句,并且还添加了检查以查看它是否是允许规则(因为在其中包含“拒绝”规则只会造成混淆)。 我用过? 这是Where的别名,以及%ForEach-Object的别名。 管子后面可以有一个自然的线制动器,所以我这样做是为了便于阅读。 我在每一行中都包含了我正在做的事情的评论,但是如果其中任何一项令人困惑,请告诉我您需要澄清的内容。

    $Groups = $ACLs.Access | #Expand the Access property
        ?{ $_.IsInherited -eq $false -and $_.AccessControlType -eq 'Allow' -and $_.IdentityReference -notmatch 'BUILTIN|NT AUTHORITY|CREATOR|-----|Identity'} | #Only instances that allow access, are not inherited, and aren't a local group or special case
        %{$_.IdentityReference -replace 'JAC.*?\\'} | #Expand the IdentityReference property, and replace anything that starts with JAC all the way to the first backslash (likely domain name trimming)
        Select -Unique #Select only unique values

现在,我们将循环浏览各个组,首先输出组名和一行。

    ForEach ($Group in $Groups){
        $Group
        '-'*$Group.Length

对于每个组,我将通过检查哈希表上的键列表来查看是否已经知道谁在其中。 如果找不到该组,则将查询AD并将该组添加为键,并将成员添加为关联值。

        If($ADGroups.Keys -notcontains $Group){
            $Members = Get-ADGroupMember $Group -Recursive -ErrorAction Ignore | % Name
            $ADGroups.Add($Group,$Members)
        }

现在我们确定我们有小组成员,我们将显示他们。

        $ADGroups[$Group]

我们可以关闭与组有关的ForEach循环,由于这是当前文件夹循环的结束,因此我们将在输出中添加空白行,并也关闭该循环

    }
    "`n"
}

所以我写下来,然后在我的C:\\ temp文件夹中运行它。 它确实告诉我我需要清理该文件夹,但更重要的是,它告诉我大多数文件夹没有任何非继承权限,因此它只为我提供带下划线,空白行的路径,并移到下一个文件夹,所以我遇到了很多类似的事情:

C:\Temp\FolderA
---------------

C:\Temp\FolderB
---------------

C:\Temp\FolderC
---------------

这对我似乎没有用。 如果是您,请使用上面的行,因为我有这些行。 我个人选择获取ACL,检查组,然后如果没有组,则移至下一个文件夹。 以下是该产品。

Param([string]$filePath = $PWD)

$Version=$PSVersionTable.PSVersion
if ($Version.Major -lt 3) {Throw "Powershell version out of date. Please update powershell." }

#Create an empty hashtable to track groups
$ADGroups = @{}

#Get a recursive list of folders and loop through them
ForEach($Folder in (Get-ChildItem $filePath -Recurse -Directory)){
    # Get ACLs for the folder
    $ACLs = Get-Acl -Path $Folder.FullName

    #Do a bunch of filtering to just get AD groups
    $Groups = $ACLs | 
        % Access | #Expand the Access property
        where { $_.IsInherited -eq $false -and $_.AccessControlType -eq 'Allow' -and $_.IdentityReference -notmatch 'BUILTIN|NT AUTHORITY|CREATOR|-----|Identity'} | #Only instances that allow access, are not inherited, and aren't a local group or special case
        %{$_.IdentityReference -replace 'JAC.*?\\'} | #Expand the IdentityReference property, and replace anything that starts with JAC all the way to the first backslash (likely domain name trimming)
        Select -Unique #Select only unique values

    #If there are no groups to display for this folder move to the next folder
    If($Groups.Count -eq 0){Continue}

    #Display Folder Path
    $Folder.FullName
    #Put a dashed line under the folder path (using the length of the folder path for the length of the line, just to look nice)
    '-'*$Folder.FullName.Length

    #Loop through each group and display its name and users
    ForEach ($Group in $Groups){
        #Display the group name
        $Group
        #Add a line under the group name
        '-'*$Group.Length
        #Check if we already have this group, and if not get the group from AD
        If($ADGroups.Keys -notcontains $Group){
            $Members = Get-ADGroupMember $Group -Recursive -ErrorAction Ignore | % Name
            $ADGroups.Add($Group,$Members)
        }
        #Display the group members
        $ADGroups[$Group]
    }
    #output a blank line, for some seperation between folders
    "`n"
}

我已经设法让这个为我工作。

我编辑了以下部分以显示用户的名称和用户名。

$Members = Get-ADGroupMember $Group -Recursive -ErrorAction Ignore | % Name | Get-ADUser -Property DisplayName | Select-Object DisplayName,Name | Sort-Object DisplayName

这真的很好用,但是如果在文件夹结构中重复它,是否有办法让它停止列出相同的组访问权限?

例如,“\\example1\\example2”被分配了一个名为“group1”的组,我们有以下文件夹结构:

\\example1\example2\folder1 
\\example1\example2\folder2
\\example1\example2\folder1\randomfolder
\\example1\example2\folder2\anotherrandomfolder

所有文件夹都被分配到组“group1”,当前代码将列出每个目录的组和用户,即使它们是相同的。 如果在目录结构中重复,是否可以让它只列出组和用户一次?

-notcontains似乎对我不起作用

如果这有意义吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM