【发布时间】:2021-04-20 22:31:45
【问题描述】:
我正在尝试为文件夹、子文件夹和文件设置修改权限,并排除其中的特定文件夹。 我尝试了两种方法,但每种方法都有一个小问题。 在第一个上,我成功应用了所有权限,但“临时”文件夹排除不起作用。它也获得了权限:
$Acl = Get-Acl "C:\Users\John\Desktop\test"
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
$Acl.SetAccessRule($Ar)
Set-Acl "C:\Users\John\Desktop\test" $Acl -Exclude "C:\Users\John\Desktop\test\temp"
第二个选项是排除文件夹,但也没有为根文件夹设置权限。 我知道他是因为 get-childitem,但我不明白如何在代码中也包含根文件夹:
$Acl = Get-Acl "C:\Users\John\Desktop\test"
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
$Acl.SetAccessRule($Ar)
get-childitem "C:\Users\John\Desktop\test\" -Recurse -Exclude "C:\Users\John\Desktop\test\temp" | Set-Acl -AclObject $Acl
感谢您的帮助。 谢谢!
【问题讨论】:
-
第一个,试试
-Exclude temp。 -
第二个:
$dir = "C:\Users\John\Desktop\test\"; $(get-item $dir; get-childitem $dir -recurse -Exclude "C:\Users\John\Desktop\test\temp") | Set-Acl -AclObject $Acl。来自stackoverflow.com/a/29157250/7571258 -
考虑一下,在第一种情况下,
-Exclude无法工作,因为您通过了ContainerInherit。所以“temp”子目录无论如何都会继承ACL。 -
非常感谢 zett42!不幸的是,对于第二个选项-该文件夹仍未排除:(
-
您必须为根文件夹设置不同的访问规则,以防止继承:
New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "Modify", "ObjectInherit", "NoPropagateInherit", "Allow")
标签: powershell permissions acl