gpt4 book ai didi

powershell - 用例子为初学者讲解PowerShell中的ACL对象属性 "SetAccessRuleProtection"

转载 作者:行者123 更新时间:2023-12-04 15:10:01 27 4
gpt4 key购买 nike

我无法准确理解 SetAccessRuleProtection 属性在 PowerShell 中的作用。

如果我们看一下微软的文档 here .

Sets or removes protection of the access rules associated with this ObjectSecurity object. Protected access rules cannot be modified by parent objects through inheritance.

isProtected

Type: System.Boolean

true to protect the access rules associated with this ObjectSecurity object from inheritance; false to allow inheritance.

preserveInheritance

Type: System.Boolean

true to preserve inherited access rules; false to remove inherited access rules. This parameter is ignored if isProtected is false.



好吧,它有点自我解释,但它并不总是有效。拿一些像这样的简单代码:
$fpath = "\\server\grandfather\parent"
New-Item -ItemType directory -Path $fpath
$acl = Get-Acl $fpath
$acl.SetAccessRuleProtection($False,$True)
Set-Acl $fpath $acl | Out-Null

根据文档,这意味着“\\server\grandfather\parent”从其父级继承了所有内容,在这种情况下将是“grandfather”,因为“isProtected”参数设置为 false 以允许继承。此外,由于preserveInheritance 设置为true,它保留从“祖父”那里获得的继承规则。

让我们再看一些代码:
$fpath = "\\server\grandfather\parent"
New-Item -ItemType directory -Path $fpath
$acl = Get-Acl $fpath
$acl.SetAccessRuleProtection($False,$True)
Set-Acl $fpath $acl | Out-Null
$spath = "\\server\grandfather\parent\son"
New-Item -ItemType directory -Path $spath
$acl = Get-Acl $spath
$acl.SetAccessRuleProtection($True,$False)
Set-Acl $spath $acl | Out-Null

在这种情况下,“\\server\grandfather\parent\son”没有继承任何东西,因为它不受继承的保护。如果确实如此(但这是不可能的),它会删除其所有继承的属性。这将使文件夹基本上无法访问,除非所有者。

这样对吗?你能举出更多的例子吗
$acl.SetAccessRuleProtection($True,$True)
$acl.SetAccessRuleProtection($False,$False)
$acl.SetAccessRuleProtection($True,$False)
$acl.SetAccessRuleProtection($False,$True)

最佳答案

例子

实际上,只有3种情况:

$acl.SetAccessRuleProtection($True, $True)
$acl.SetAccessRuleProtection($True, $False)
$acl.SetAccessRuleProtection($False, X) -- preserveInheritance is ignored when isProtected is false

所以你的第一个例子是除了你的“Furthermore...”部分之外的描述。在该示例中,preserveInheritance 没有任何作用。

您的第二个示例是正确的,所有内容都已删除,只有所有者才能访问。

与您之前的示例一致,(True, True) 场景的示例是
$fpath = "\\server\grandfather\parent"
New-Item -ItemType directory -Path $fpath
$acl = Get-Acl $fpath
$acl.SetAccessRuleProtection($False, $True)
Set-Acl $fpath $acl | Out-Null
$spath = "\\server\grandfather\parent\son"
New-Item -ItemType directory -Path $spath
$acl = Get-Acl $spath
$acl.SetAccessRuleProtection($True, $False)
Set-Acl $spath $acl | Out-Null
$dpath = "\\server\grandfather\parent\daughter"
New-Item -ItemType directory -Path $spath
$acl = Get-Acl $spath
$acl.SetAccessRuleProtection($True, $True)
Set-Acl $spath $acl | Out-Null

这将导致子文件夹与父文件夹具有完全相同的权限,但至关重要的是,它们不会被标记为继承的。它们将在子文件夹上明确标记为权限。

为什么这些例子并不合适

上面的示例是全新的文件夹,因此没有超出默认权限的任何内容。此外,由于默认权限是自动继承的,所有子文件夹都将具有相同的权限集。在这种情况下使用 Set-Acl 和 SetAccessRuleProtection,而不修改 acl,并没有真正提供多少。

SetAccessRuleProtection 的一个典型用途是控制修改 ACL 时对现有 AccessRules 的处理。即,将新用户添加到具有修改权限的 ACL。你想要_____吗
  • 添加新用户并保留所有当前权限:SetAccessRuleProtection(False, X)
  • 添加新用户并删除所有继承的权限:SetAccessRuleProtection(True, False)
  • 添加新用户并将所有继承的权限转换为显式权限:SetAccessRuleProtection(True, True)

  • 在上面的示例中,SetAccessRuleProtection 通过添加对 ACL 中现有权限的更多控制而变得更加有用。

    脚注

    实际上,如果您希望打破对子文件夹的其他子文件夹的默认权限的继承,但为子文件夹保留它们,那么此答案开头给出的 (True, True) 示例可能对全新文件夹很有用。

    关于powershell - 用例子为初学者讲解PowerShell中的ACL对象属性 "SetAccessRuleProtection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787843/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com