使用PowerShell仅删除一个继承权限

时间:2015-11-01 15:07:57

标签: powershell inheritance acl

我正在尝试编写一个脚本,该脚本可以删除仅具有继承权限的文件夹中的一个(例如Everyone)的访问权限。

其他继承权限应保持不变。我可以删除继承权限,然后删除该组的访问权限,但继承会被破坏。由于没有继承被破坏的子文件夹,我不想在此操作后启用继承。

如何在不弄乱剩余权限的情况下删除此组?

3 个答案:

答案 0 :(得分:6)

您不能(按设计)删除继承的权限,而不会弄乱剩余的权限"。

可以做的是

  1. 禁止继承,但保留已经继承的规则
  2. 删除继承后删除/修改EVERYONE ACE
  3. 像这样:

    $FilePath = "C:\parentFolder\childItem.ext"
    $FileACL  = Get-Acl $FilePath
    
    # Remove inheritance but preserve existing entries
    $FileACL.SetAccessRuleProtection($true,$true)
    Set-Acl $FilePath -AclObject $FileACL
    
    # Retrieve new explicit set of permissions
    $FileACL  = Get-Acl $FilePath
    
    # Retrieve "everyone" rule
    $EveryoneRule = $FileACL.GetAccessRules($true,$true,[System.Security.Principal.NTAccount]) | Where-Object {$_.IdentityReference -eq [System.Security.Principal.NTAccount]"EVERYONE"}
    
    # Remove it - or modify it and use SetAccessRule() instead
    $FileACL.RemoveAccessRule($EveryoneRule)
    
    # Set ACL on file again
    Set-Acl $FilePath -AclObject $FileACL
    

答案 1 :(得分:0)

要在不禁用继承的情况下删除组或用户ACE,请使用CACLS 文件夹 / E / R 组/用户。我知道CACLS已被弃用,但在使用iCacls或SETACL时我没有发现任何等效内容。

答案 2 :(得分:0)

实际上您不必删除继承。

可以只消除这个小错误。有同样的错误,它在 Windows 2016 文件服务器上成功。

我稍微修改了 Mathias R. Jessen 的脚本。如果您只想对一个文件夹执行此操作,请将“$folders = Get-Childitem”替换为“$filepath = Get-Item”并仅使用 foreach 循环内的命令。

以管理员身份打开 Powershell

$folders = Get-ChildItem "C:\Path\To\Folder" | where {$_.psiscontainer -eq $true}

foreach ($FilePath in $folders)
{
    $FileACL  = Get-Acl $FilePath.FullName
    $EveryoneRule = $FileACL.GetAccessRules($true,$true,[System.Security.Principal.NTAccount]) | Where-Object {$_.AccessControlType -eq "Deny"}
    $FileACL.RemoveAccessRule($EveryoneRule)
    Set-Acl $FilePath.FullName -AclObject $FileACL
}
相关问题