通过 Powershell 将 AD 组添加到大型 Public 驱动器

Adding an AD Group to a large Public drive via Powershell

我们有一个大型文件共享,其中包含大约 1tb 的数据。 以下位置下有大约 600 个文件夹。 F:\数据
任务是为数据文件夹内的每个文件夹分配一个特定的 AD 组读取权限,子文件夹无关紧要。

我想看看下面的脚本是否是最好的方法? 我担心的是这是一个文件服务器,我不想破坏任何东西 或搞砸任何权利,也不确定脚本是否是 运行 和他们的 是否打开文件会导致错误。

我已经在测试环境中尝试 运行 这个脚本并且效果很好,但是没有错误日志,即使它停止在某个地方我也可以检查。

可能是我多虑了,只是想看看有没有人遇到过这样的事情?

$StartingPath = "PATH"
$Right = "Read"
$Principal = "Domain\ADGroup"

$Rule = New-Object System.Security.AccessControl.FileSystemAccessRule($Principal,$Right,"Allow")

foreach ($Folder in $(Get-ChildItem -Directory $StartingPath -Recurse)) {
$Acl=Get-Acl $Folder.FullName
$Acl.SetAccessRule($Rule)
Set-Acl $folder.Fullname $Acl
}

您需要尝试继承和传播(为此使用您的测试环境)并使用带有 5 个参数的重载方法为此创建新的访问规则。

这样,您只需将新规则添加到主数据共享文件夹,而不必遍历所有子文件夹。

# FileSystemRights:  https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesystemrights
# Inheritance flags: https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.inheritanceflags
# Propagation flags: https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.propagationflags

$Principal  = "TheADGroupWithReadPermissions"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Principal, "Read", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl = Get-Acl "F:\Data"
$acl.SetAccessRule($accessRule)
Set-Acl -Path "F:\Data" -ACLObject $acl

AddAccessRule()SetAccessRule()的区别:

AddAccessRule SetAccessRule
This method will add this access rule to the ACL. If a user or group has Modify permission and we use AddAccessRule() to create a new rule with Read permission the user or group will still also have Modify permissions. This method removes any existing access and replaces that access with the specified rule. If a user or group has Modify permission and a new rule is created using SetAccessRule() specifying Read permission, that user or group will now only have Read permission.