尝试复制继承的权限时出错

Error when attempting to copy inherited permissions

我希望对一组子文件夹禁用 ACL 继承,同时保留现有权限。

为此,我运行这个片段:

gci | % {
  $Acl = Get-Acl $_
  $Acl.SetAccessRuleProtection($true, $true)
  Set-Acl $_ $Acl
}

对于每个子文件夹,出现一个错误:

Set-Acl : Cannot set the ACL because the method that it needs to invoke, SetSecurityDescriptor, does not exist.

我找到了 ,但它不是完全相同的副本。 OP打算清除所有权限;我想保留他们。

另外:OP 说 "I got rid of the error message," 但没有透露他是如何做到的

如何使用 PowerShell 完成此操作?

这应该适合你:

Get-ChildItem | ForEach-Object {
    $Acl = Get-Acl $_
    $Acl.SetAccessRuleProtection($true,$false) #Set folder inheritance to off
    Set-Acl $_ -AclObject $Acl    
}

Set-Acl 存在一些问题。我通常尝试依靠 .NET 来进行 ACL 工作:

try {
    $FileSystemObject = (Get-Item '.\accessibilitycpl.dll')
    $Acl = $FileSystemObject.GetAccessControl()
    $Acl.SetAccessRuleProtection($true,$false)
    $FileSystemObject.SetAccessControl($Acl)
} catch {
    ## Catch exceptions!
}