将 ACL System.Security.AccessControl.FileSystemAccessRule 设置为多个用户?

Set ACL System.Security.AccessControl.FileSystemAccessRule to multiple users?

在使用System.Security.AccessControl.FileSystemAccessRule设置权限时,是否可以直接在某种数组中指定超过 1 个用户?

示例代码:

$acl = Get-Acl perm.txt
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("Desktop\David","Read","Allow")
$acl.SetAccessRule($AccessRule) 
Get-ChildItem -Path "C:\Users\David\Scripts\test\testfiles" -Recurse -Filter "*testing1234*" -File | Set-Acl -AclObject $acl

这工作正常并为用户 David 设置了权限。但是假设我想为用户 David 和用户 Lena 设置权限,是否可以指定多个用户,例如:

$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("Desktop\David","Read","Allow","Desktop\Lena","Read","Allow")

或者是否必须为用户生成唯一的 ACL?

使用循环将多个 ACE 添加到 ACL:

$users = 'foo', 'bar', 'baz'

foreach ($user in $users) {
    $ace = New-Object Security.AccessControl.FileSystemAccessRule ("Desktop${user}", 'Read', 'Allow')
    $acl.AddAccessRule($ace)
}

您可能还想避免将 ACL 递归地应用于文件夹树。将 ACL 应用到最顶层的文件夹,让 ACL 继承处理其余部分。