【发布时间】:2015-01-27 21:18:32
【问题描述】:
我正在尝试对一个非常大的文件夹树(多个服务器/共享)应用权限。我的脚本可以运行,但效率不高,而且运行时间太长,因为它是基于每个用户和每个文件夹设置的。
我有一个包含三列用户 ID、路径和偏移量的 csv 文件。下面的例子
Domain\OneUser,\\\Server\Share\1\2\3\4\5\6,5<BR>
Domain\TwoUser,\\\Server\Share\1\2\3\4\5\6,5<BR>
Domain\OneUser,\\\Server\Share\1\2\A\B\C\D,5<BR>
Domain\ThreeUser,\\\Server\Share\1\2\A\B\C,5<BR>
Domain\TwoUser,\\\Server\Share\1\2\3\4\5,5
脚本读取文件,然后使用偏移量设置每个对象的权限。在此示例中,偏移量为 5,这将使脚本在 1 处停止。它还会忽略路径中的最后一个文件夹。
在上述情况下,OneUser 将设置为 5、4、3、2、1。然后将 TwoUser 设置为 5、4、3、2、1。然后 OneUser 将设置在 C、B、A、2、1 上。其中一些是重复工作,如第 3 行(在已设置后设置为 2 和 1)。
我不需要为每个单独的文件夹设置权限,而是需要将它组合在一起,以便它可以像这样设置它,而不是设置权限 23 次,这可以在 8 中完成。
OneUser,Twouser on 5,4,3<BR>
OneUser on C<BR>
OneUser,ThreeUser on B,A<BR>
OneUser,TwoUser,ThreeUser on 2,1
我想做以下两件事之一:
-
将列表导出为 csv,如下所示:
OneUser;Twouser,\\Server\Share\1\2\3\4\5 OneUser;Twouser,\\Server\Share\1\2\3\4\
等等。
或者使用下面的脚本,对于每组唯一的文件夹/用户,修改 $objACE 和 $objACL 以包含每个文件夹的唯一用户
$objACE1 = New-Object System.Security.AccessControl.FileSystemAccessRule(**Domain\OneUser**, $colRights, $InheritanceFlag, $PropagationFlag, $objType<BR>)
$objACE2 = New-Object System.Security.AccessControl.FileSystemAccessRule(**Domain\TwoUser**, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
$objACL.AddAccessRule($objACE1)<BR>
$objACL.AddAccessRule($objACE2)
本质上,我需要在每个文件夹调用 Set-Acl 之前为每个文件夹添加所有对象,因此我只为每个文件夹应用一次权限。我还需要排除结束文件夹(权限已经存在)并告诉它在哪里停止。
有人对如何轻松完成此任务有任何想法吗?
这部分脚本是最重要的,包含我设置适当权限所需的所有设置。
#parameters for setting proper traverse/list access to 'This folder only' of parent tree
$colRights = [System.Security.AccessControl.FileSystemRights]"ReadAndExecute,Synchronize"
#turns off inheritance for this permission
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None
#ensures 'this folder only' setting of permissions - will not force propogation
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
#specifies that this is an allow access type
$objType = [System.Security.AccessControl.AccessControlType]::Allow
#reads in group/user from csv file to grant permissions for
$objUser = New-Object System.Security.Principal.NTAccount($UserName)
#builds the new access control to program on parent folders
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
#reads in current access control of current parent folder
$objACL = Get-ACL $Container
#adds the access rule to current access controls of the current parent folder
$objACL.AddAccessRule($objACE)
#writes out screen feedback on current status of the script
write-host Setting permissions on $Container for $Username
#Sets access controls on each applicable folder. This does not affect existing permissions, only adds to them.
Set-ACL $container $objACL
【问题讨论】:
-
请退后一步,描述您要解决的实际问题,而不是您认为的解决方案。你想通过这样做来达到什么目的?你需要那个“偏移量”做什么?为什么不能简单地设置文件夹上 5 级的权限,其余的留给继承?
标签: powershell csv permissions