【发布时间】:2021-04-09 18:13:27
【问题描述】:
我正在尝试编写一个脚本,该脚本可以删除一个(例如所有人)对已继承权限的文件夹的访问权限。
其他继承权限应保持不变。我可以删除继承权限,然后删除该组的访问权限,但继承随后被破坏。我不想在此操作后启用继承,因为没有继承的子文件夹被破坏。
如何在不影响其余权限的情况下删除该组?
【问题讨论】:
标签: powershell inheritance acl
我正在尝试编写一个脚本,该脚本可以删除一个(例如所有人)对已继承权限的文件夹的访问权限。
其他继承权限应保持不变。我可以删除继承权限,然后删除该组的访问权限,但继承随后被破坏。我不想在此操作后启用继承,因为没有继承的子文件夹被破坏。
如何在不影响其余权限的情况下删除该组?
【问题讨论】:
标签: powershell inheritance acl
您不能(按设计)删除继承的权限,“而不弄乱其余权限”。
你可以做的是
EVERYONE ACE像这样:
$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
【讨论】:
要在不禁用继承的情况下删除组或用户 ACE,请使用 CACLS folder /E /R group/user。我知道 CACLS 已被弃用,但在使用 iCacls 或 SETACL 时我没有找到任何等效项。
【讨论】:
其实你不必删除继承。
可以消除这个小错误。有同样的错误,它在 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
}
【讨论】: