Powershell 使用文件夹排除设置ACL

Powershell 使用文件夹排除设置ACL,powershell,permissions,acl,Powershell,Permissions,Acl,我正在尝试设置文件夹、子文件夹和文件的修改权限,并排除其中的特定文件夹。 我尝试了两种方法,但每种方法都有一个小问题。 在第一个示例中,我成功地应用了所有权限,但“temp”文件夹排除不起作用。它还可以获得权限: $Acl = Get-Acl "C:\Users\John\Desktop\test" $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone",

我正在尝试设置文件夹、子文件夹和文件的修改权限,并排除其中的特定文件夹。 我尝试了两种方法,但每种方法都有一个小问题。 在第一个示例中,我成功地应用了所有权限,但“temp”文件夹排除不起作用。它还可以获得权限:

$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
谢谢你的帮助。
谢谢大家!

我希望我做对了。现在我在家里运行了代码,所以明天我将在工作中对其进行更深入的测试。 这就是我所做的:

$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外,包含“Everyone-Modify”的所有子文件夹和文件
  • 使用“所有人-修改”-“此文件夹和文件”(特殊权限)“测试”根文件夹 我希望它能满足我的工作需要

再次感谢

对于第一个,请尝试
-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
。仔细想想,在第一种情况下,
-Exclude
无法工作,因为您传递了
容器。所以“临时”子目录将继承ACL。非常感谢您!不幸的是,对于第二个选项,文件夹仍然没有被排除:(您必须为根文件夹设置不同的访问规则,以防止继承:
newobjectsystem.Security.AccessControl.FileSystemAccessRule(“Everyone”、“Modify”、“ObjectInherit”、“NoPropagateInherit”、“Allow”)