Powershell 递归地向文件夹授予域用户/组权限

Powershell 递归地向文件夹授予域用户/组权限,powershell,Powershell,写在下面的代码,用于向windows 2016中的文件夹授予域用户权限。在输出中,我可以看到用户添加了文件夹权限,但没有添加任何权限,尽管我提到了授予完全控制访问权 $rule=new-object System.Security.AccessControl.FileSystemAccessRule("domain\group","FullControl","Allow") foreach ($file in $(Get-ChildItem "G:\usr" -recurse)) { $

写在下面的代码,用于向windows 2016中的文件夹授予域用户权限。在输出中,我可以看到用户添加了文件夹权限,但没有添加任何权限,尽管我提到了授予完全控制访问权

$rule=new-object System.Security.AccessControl.FileSystemAccessRule("domain\group","FullControl","Allow")

foreach ($file in $(Get-ChildItem "G:\usr" -recurse)) 
{
  $acl=get-acl $file.FullName

  $acl.SetAccessRule($rule)

  set-acl $File.Fullname $acl
} 

对于递归权限,您需要设置
ContainerInherit、ObjectInherit

下面是一个示例(请注意,这不是我的代码):


有关更多详细信息,请参阅上文提到的更新代码。现在它正在更新权限,但也会抛出下面的异常。使用“1”参数调用“SetAccessRule”时出现异常:“无法设置任何标志。参数名称:inheritanceFlags”位于C:\Users\username\Desktop\test.ps1:7 char:3+$acl.SetAccessRule($rule)+~~~~~~~~~~~~~~~~~~~+CategoryInfo:NotSpecified:(:)[],MethodInvocationException+FullyQualifiedErrorId:ArgumentException调用带有“1”参数的“SetAccessRule”异常:“无法设置任何标志。
$Path = "C:\temp\New folder"
$Acl = (Get-Item $Path).GetAccessControl('Access')
$Username = "Domain\User"
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($Username, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl