重置并向根文件夹,子文件夹和文件添加新权限?

时间:2018-01-24 16:31:22

标签: powershell powershell-v3.0

我有一个像这样的文件夹结构:

Case1
  ===== Accounting
  ===== Audits
  ===== Data
Case2
  ===== Accounting
  ===== Audits
  ===== Data
...

我在共享驱动器上有数千个Case(根文件夹),多年来,权限变得一团糟,所以我需要遍历所有文件夹,子文件夹和文件并设置适当的权限。这就是我需要设置的内容

Root folder (case1, case2....caseN)
   case_admin (Full Control)
   case_root (List Folder Contents)

Accounting
    case_admin (Full Control)
    case_accounting (Modify)

Audits
    case_admin (Full Control)
    case_auditing (Modify)

Data
    case_admin (Full Control)
    case_data (Modify)

我不确定是否应首先删除所有权限,因为这不会阻止我添加新权限,但我也认为删除所有内容并只添加我需要的权限会更容易。到目前为止,这是基于一个根文件夹的。

$filepath = 'L:\case1'
$user     = 'domain\userTORemove'

$folders = Get-ChildItem $filePath -Recurse -Directory

foreach ($folder in $folders) {
    $acl = Get-Acl -Path $folder.FullName

    foreach ($access in $acl.Access) {
        if ($access.IdentityReference.Value -eq $user) {
            $acl.RemoveAccessRule($access) | Out-Null
        }

        if($folder.FullName -eq "Accounting")
        {
             $ace = New-Object System.Security.Accesscontrol.FileSystemAccessRule ("case_admin", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
   $acl.AddAccessRule($ace)

 $ace = New-Object System.Security.Accesscontrol.FileSystemAccessRule ("case_root", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
 $acl.AddAccessRule($ace)

Set-Acl -Path 'L:\case1' -AclObject $acl
        }

    }

    Set-Acl -Path $folder.FullName -AclObject $acl
}

因此,简而言之,我需要遍历所有根文件夹和子文件夹,并在根文件夹上设置权限,然后在子文件夹上设置权限,权限基于子文件夹的名称。

我无法知道每个文件夹上的权限,所以如果我执行条件删除,我可能会错过一些东西,所以这就是为什么我提到首先删除所有权限

1 个答案:

答案 0 :(得分:0)

要记住的一件事是Get-Acl基本上根据文件/文件夹/当前的访问规则创建ACE的模板。然后,您可以随意修改该模板(删除所有人,并添加回特定用户/组),并且在您使用Set-Acl将该模板应用于原始对象之前,它不会对原始对象产生任何影响。

所以,除了默认项目之外,我会删除所有内容。我会在名称(本地管理员组,域管理员,无论如何)中使用“管理员”以及SYSTEM帐户留下任何内容。我也会留下'OWNER CREATOR',但那只是个人喜好。我会坚持用我的榜样。

如果这是我,我会遍历caseX文件夹,然后对于每个文件夹,我将遍历子文件夹,除了上面提到的3个帐户之外的所有文件,然后在文件夹的相应组中添加回来。为了获得该组,我将在循环之前设置一个哈希表,将文件夹名称定义为关联的组名。子文件夹完成后,我基本上会对根案例文件夹做同样的事情,然后移动到下一个案例文件夹。

# Setup a hashtable to lookup group name by folder name
$FolderGroup = @{
    'Accounting' = 'case_Accounting'
    'Audit' = 'case_Auditing'
    'Data' = 'case_Data'
}

# Loop through all root case folders
ForEach($CaseFolder in (GCI L:\Case* -Directory))
{
    #Loop through subfolders of current case folder
    ForEach($Folder in (GCI $CaseFolder -Directory)){
        $ACL = Get-Acl $Folder
        # Remove access for any user other than Administrators, the Creator/Owner, and the local SYSTEM account
        $ACL.Access | ?{$_.IdentityReference -notmatch "Administrators|Creator owner|system"}|%{$ACL.RemoveAccessRule($_)|Out-Null}
        # Add the department back
        $ACE = New-Object New-Object System.Security.Accesscontrol.FileSystemAccessRule ($FolderGroup[($Folder.Name)], "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
        $ACL.AddAccessRule($ACE)

        # Apply the modified ACL to the directory
        Set-Acl $Folder $ACL
    }

    $ACL = Get-Acl $CaseFolder
    # Remove access for any user other than Administrators, the Creator/Owner, and the local SYSTEM account
    $ACL.Access | ?{$_.IdentityReference -notmatch "Administrators|Creator owner|system"}|%{$ACL.RemoveAccessRule($_)|Out-Null}
    # Add the admin and root ACE's
    $AdminACE = New-Object New-Object System.Security.Accesscontrol.FileSystemAccessRule ('case_admin', "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
    $RootACE = New-Object New-Object System.Security.Accesscontrol.FileSystemAccessRule ('case_root', "Read,Traverse", "None", "None", "Allow")
    $ACL.AddAccessRule($AdminACE)
    $ACL.AddAccessRule($RootACE)

    # Apply modified ACL to root case folder
    Set-Acl $CaseFolder $ACL
}