PowerShell设置高级NTFS权限

12

我正在尝试应用在Windows安全设置的“高级”选项卡中定义的NTFS权限。一个ACL $Rule 是针对仅此文件夹,另一个是针对仅子文件夹和文件

正如您下面所看到的,这些权限已经被大量修改:

(Get-Acl 'L:\Test\Beez\RAPJOUR\Appels List\Correct').Access

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : CreateFiles, AppendData, DeleteSubdirectoriesAndFiles, ReadAndExecute, Synchronize
AccessControlType : Allow
IdentityReference : Domain\Dirk
IsInherited       : False
InheritanceFlags  : None
PropagationFlags  : None

FileSystemRights  : DeleteSubdirectoriesAndFiles, Modify, Synchronize
AccessControlType : Allow
IdentityReference : Domain\Dirk
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : InheritOnly

这只有这个文件夹

  • 除了完全控制、写入属性、写入扩展属性、删除、更改权限和获取所有权之外,一切都是开启的。

仅限子文件夹和文件

  • 除了完全控制、更改权限和获取所有权之外,一切都是开启的。

这是我用来应用权限的代码片段。在这种情况下,它必须定义在Change部分中:

 $f = 'L:\Test\Beez\RAPJOUR\Appels List\Wrong'
 $ADobject = 'Domain\User'
 $acl = Get-Acl $f

 $Grant = 'Change'
    # Remove user/group first
    $rule = New-Object system.security.AccessControl.FileSystemAccessRule("$ADobject","Read",,,"Allow")
    $acl.RemoveAccessRuleAll($rule)

    # Add read permissions
    if ($Grant -eq 'ReadAndExecute') {
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", "ReadAndExecute", "ContainerInherit, ObjectInherit", "None", "Allow")
    }

    if ($Grant -eq 'Change') {
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", "Modify", "ContainerInherit, ObjectInherit", "Synchronize", "Allow  DeleteSubdirectoriesAndFiles")
        $acl.AddAccessRule($rule)
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", "AppendData", "ContainerInherit, ObjectInherit", "ReadAndExecute","Synchronize", "Allow  CreateFiles","DeleteSubdirectoriesAndFiles")
    }

    if ($Grant -eq 'Modify') {
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", "Modify", "ContainerInherit, ObjectInherit", "None", "Allow")
    }

    if ($Grant -eq 'FullControl') {
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
    }

    if ($Grant -eq 'ListFolderContents') {
        $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", "ReadAndExecute", "ContainerInherit", "None", "Allow")
    }

$acl.AddAccessRule($rule)
Set-Acl $f $acl

我似乎无法正确使用语法...谢谢你的帮助。

多亏了这篇文章,我已经找到了以下内容:

  • '仅子文件夹和文件': "ContainerInherit, ObjectInherit", "InheritOnly"
  • '仅此文件夹': "None", "InheritOnly"
2个回答

20
在Windows中,对象访问权限是通过访问控制列表 (ACL) 控制的,基本上由一系列访问控制项 (ACE) 组成。每个 ACE 是一组属性,控制是否允许或拒绝访问、ACE 应用于谁、ACE 是否从父对象继承以及是否应该被子对象继承。
如果您查看FileSystemAccessRule 类的文档,您会发现“完整”构造函数需要 5 个参数:
- IdentityReference/String: 标识受信任者 (用户、组、计算机等) 的对象或字符串,ACE 将应用于该对象。 - FileSystemRights: 要授予或拒绝的实际权限。 - InheritanceFlags: 控制此对象类型所继承的对象类型 (容器、叶对象或无) 的标志。 - PropagationFlags: 控制权限传播的标志。标志InheritOnly免除当前对象接收 ACE。标志NoPropagateInherit限制继承到即时子对象。 - AccessControlType: ACE 的类型(允许或拒绝)。
现在,如果您想给特定受信任者分配多个访问权限,则可以使用单独的 ACE 进行操作:
$acl  = Get-Acl $path
$ace1 = New-Object Security.AccessControl.FileSystemAccessRule 'DOMAIN\user',
        'ListDirectory', 'ContainerInherit, ObjectInherit', 'InheritOnly',
        'Allow'
$acl.AddAccessRule($ace1)
$ace2 = New-Object Security.AccessControl.FileSystemAccessRule 'DOMAIN\user',
        'ReadAttributes', 'ContainerInherit, ObjectInherit', 'InheritOnly',
        'Allow'
$acl.AddAccessRule($ace2)
...

或者将权限以逗号分隔的字符串形式提供:

$acl = Get-Acl $path
$ace = New-Object Security.AccessControl.FileSystemAccessRule 'DOMAIN\user',
       'ListDirectory, ReadAttributes, ...', 'ContainerInherit,  ObjectInherit',
       'InheritOnly', 'Allow'
$acl.AddAccessRule($ace)

请注意,您不能使用相同的ACE授予和拒绝权限。如果您想否决特定的访问权限,则需要使用单独的ACE来实现:

$acl  = Get-Acl $path
$ace1 = New-Object Security.AccessControl.FileSystemAccessRule 'DOMAIN\user',
        'Modify', 'ContainerInherit, ObjectInherit', 'InheritOnly',
        'Allow'
$acl.AddAccessRule($ace1)
$ace2 = New-Object Security.AccessControl.FileSystemAccessRule 'DOMAIN\user',
        'CreateDirectories', 'ContainerInherit, ObjectInherit', 'InheritOnly',
        'Deny'
$acl.AddAccessRule($ace2)
...

还需注意,显式的权限优先于继承的权限,而Deny优先于Allow


感谢您的帮助,Ansgar。这意味着我们不需要将它放在Array格式中,但是也可以接受它作为字符串,格式为'Value1,Value2,Value3,...'。 很高兴知道 :) - DarkLite1
1
稍作更正: InheritanceFlags 控制哪些类型的子对象将继承 ACE(对于文件系统,这些标志控制“应用于:”下拉框中的子文件夹和/或文件部分)。 PropagationFlags 控制 ACE 是否应用于对象本身和/或 ACE 是否应用于孙子代(对于文件系统,这些标志控制“应用于:”框中的“此文件夹”部分,以及屏幕截图中“仅将这些权限应用于此容器内的对象和/或容器”复选框)。 - Rohn Edwards
@RohnEdwards 确实。感谢您的纠正。已修复。 - Ansgar Wiechers

5
你知道在解决世界问题时会发生什么。当你发布问题后,5分钟后你就会找到答案...
感谢Frode F.在另一个问题上的回答,我找到了自己问题的解决方法。我需要复制$Correct.AccessFileSystemRights行的输出,并将其粘贴到Array中,如下所示:
 $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", @("CreateFiles", "AppendData", "DeleteSubdirectoriesAndFiles"," ReadAndExecute", "Synchronize"), "None", "InheritOnly", "Allow") # This folder only   
 $acl.AddAccessRule($rule)
 $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$ADobject", @("DeleteSubdirectoriesAndFiles", "Modify", "Synchronize"), "ContainerInherit, ObjectInherit", "InheritOnly", "Allow") # Subfolders and files only

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接