我正在尝试在 PowerShell 中模拟我通常在 Windows 文件夹属性窗口中执行的操作,可从以下位置获取:文件夹属性 → 安全性 → 高级 → 权限 → 更改权限...
在该 GUI 中,有以下复选框
- 包括从此对象的父对象继承的权限
- 用该对象的可继承权限替换所有子对象权限 - 我相信对应于
PropagationFlag
=None
当您单击“添加/编辑...”按钮时,您会看到一个包含以下选项的下拉列表,其中每个选项对应的 InheritanceFlags
是什么?我已经填写了通过实验找到的内容
- 仅此文件夹 -
无
- 此文件夹的子文件夹和文件
- 此文件夹和子文件夹
- 此文件夹和文件
- 仅限子文件夹和文件 -
ContainerInherit、ObjectInherit
- 仅限子文件夹 -
ContainerInherit
- 仅限文件 -
ObjectInherit
哪些标志与复选框相对应仅将这些权限应用于此容器内的对象和/或容器?
我还确定这些 PropagationsFlags
的含义:
- InheritOnly(仅当当前子对象设置为继承时,ACE 才会传播到所有当前子对象)
- NoPropagateInherit(ACE 不会传播到任何当前子对象)
- 无(此 ACE 传播到所有子对象,覆盖它们之前的内容并打开它们的继承)
我想了解如何为现有用户/组添加额外权限,或向文件夹的权限添加额外用户/组,并使用“此文件夹子文件夹和文件”进行传播”,但不是“用该对象的可继承权限替换所有子对象权限”,以防出于充分原因故意有一个具有其他特殊权限的子文件夹,例如用户配置文件中的符号链接(symbolic link)文件夹。
我目前正在使用的代码如下。最终,我将在多个域中的许多计算机上的许多不同文件夹上使用它,包括 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"
最佳答案
“应用到”值由 InheritanceFlags
的组合定义和 PropagationFlags
(实际上定义了如何限制传播)。以下概述了哪些值生成哪些“应用于”设置(ContainerInherit
和 ObjectInherit
缩写为 CI
和 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
有关传播规则的更详细说明,请参阅 here正如@CB提到的。在您问题的评论中。
关于powershell - 使用 Set-Acl 定义权限应用的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32348843/