【问题标题】:Set-ACL with folder exclude带有文件夹排除的设置 ACL
【发布时间】: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


【解决方案1】:

我希望我做对了。此时我在家里运行代码,所以明天我会在工作中更深入地测试它。 这就是我所做的:

$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 "TempFolder" | Set-Acl -AclObject $Acl

##########################################################

$AclRoot = Get-Acl "C:\Users\John\Desktop\Test"
$ArRoot = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "Modify", "ObjectInherit", "NoPropagateInherit", "Allow")
$AclRoot.SetAccessRule($ArRoot)
Get-Item "C:\Users\John\Desktop\Test" | Set-Acl -AclObject $AclRoot

结果是:

  • 除 TempFolder 外,所有带有“所有人 - 修改”的子文件夹和文件。
  • “测试”根文件夹与“所有人 - 修改” - “此文件夹和文件”(特殊权限) 我希望它能满足我的工作需求。

再次感谢!

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多