2

我正在尝试在 PowerShell 中模拟我通常在 windows 文件夹属性窗口中执行的操作:文件夹属性 → 安全性 → 高级 → 权限 → 更改权限...

在那个 GUI 中有勾选框

  • 包括来自该对象的父级的可继承权限
  • 用该对象的可继承权限替换所有子对象权限 - 我相信对应于PropagationFlag=None

当您单击添加/编辑...按钮时,您会看到一个包含以下选项的下拉列表,InheritanceFlags每个选项对应的是什么?我已经填写了我通过实验找到的那些

  • 仅此文件夹 -None
  • 此文件夹的子文件夹和文件
  • 此文件夹和子文件夹
  • 此文件夹和文件
  • 仅子文件夹和文件 -ContainerInherit, ObjectInherit
  • 仅子文件夹 -ContainerInherit
  • 仅文件 -ObjectInherit

哪些标志与复选框仅将这些权限应用于此容器中的对象和/或容器对应?

我还确定这些PropagationsFlags意味着:

  • InheritOnly(仅当将 ACE 设置为继承时,ACE 才会传播到所有当前子对象)
  • NoPropagateInherit(ACE 不会传播到任何当前子对象)
  • 无(此 ACE 传播到所有子对象,覆盖它们之前的内容并打开它们的继承)

我想了解如何为现有用户/组添加额外的权限或将额外的用户/组添加到文件夹的权限,并使用“此文件夹子文件夹和文件”而不是“将所有子对象权限替换为可从此对象继承的权限”,以防出于充分的理由而故意存在具有其他特殊权限的子文件夹,例如用户配置文件中的符号链接文件夹。

我目前正在使用的代码如下。最终,我将在多个域中的许多计算机上的许多不同文件夹中使用它,包括c:\users\{username}

function check-permissions ( $folder ) {
    $GroupName = "Domain Admins"
    if ( -not (Test-Path -LiteralPath $folder) ) {
        Write-Output "Cannot find $folder"
    } else {
        ((get-acl -literalPath $folder).access).IdentityReference.Value |
            findstr /i ($env:USERDOMAIN + "\"+ $GroupName) |
            out-null
        $result = $?
        if ( -not $result ) {
            write-output ($folder + ": adding permissions")
            #adding new permissions
            $colRights = [System.Security.AccessControl.FileSystemRights]"FullControl" 
            $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit, [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
            $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly
            $objType =[System.Security.AccessControl.AccessControlType]::Allow
            $objUser = New-Object System.Security.Principal.NTAccount($env:USERDOMAIN + "\" + $GroupName)
            $objACE  = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
            # output the ACE
            $objACE | format-list *
            #$objACL = Get-Acl -literalPath $folder    # This gets the full security info but substitutes the different user as owner, which we don't want or it will overwrite the owner info with the wrong user when we use set-acl
            $objACL = (Get-Item -literalPath $folder).GetAccessControl('Access')
            if ( -not $? ) {
                Write-Output ("Failed to get permissions on: " + $folder)
            } else {
                $objACL.AddAccessRule($objACE)
                if ( $objACL ) { #objACL exists
                    #Set-ACL -literalPath ($folder) -AclObj $objACL  # This tries to set the owner too
                    [System.IO.Directory]::SetAccessControl($folder,$objACL) # This seems to work
                } else { # $objACL is null
                    write-output "Error developing new permissions object. Leaving folder permissions alone."
                }
            }
        } else {
            write-debug ($folder + ": Permissions OK")
        }
    }
}

check-permissions "c:\temp\test\a"
4

1 回答 1

3

“应用到”值由InheritanceFlags和的组合PropagationFlags定义(实际上定义了如何限制传播)。以下概述了哪些值产生了哪些“应用到”设置(由于空间有限,分别ContainerInherit缩写ObjectInheritCI和):OI

Apply To                            Inheritance   Propagation
--------                            -----------   -----------
This folder only                    None          any
This folder, subfolders and files   CI, OI        None or NoPropagateInherit
This folder and subfolders          CI            None or NoPropagateInherit
This folder and files               OI            None or NoPropagateInherit
Subfolders and files only           CI, OI        InheritOnly
Subfolders only                     CI            InheritOnly
Files only                          OI            InheritOnly

有关传播规则的更详细描述,请参见@CB 提到的此处在对您问题的评论中。

于 2015-09-03T18:28:37.103 回答