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



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

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

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

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     = 'domainuserTORemove'
$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
}

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

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

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

因此,我会删除除默认项目之外的所有内容。我会在名称中保留任何带有"管理员"的内容(本地管理员组、域管理员等)以及系统帐户。我也会离开"所有者创造者",但这只是个人喜好。我会坚持这些作为我的例子。

如果这是我,我会遍历 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
}

最新更新