【问题标题】:Using Set-Acl and the FileSystemAccessRule.RemoveAccessRule method not working使用 Set-Acl 和 FileSystemAccessRule.RemoveAccessRule 方法不起作用
【发布时间】:2021-02-10 19:20:54
【问题描述】:

总结:

我正在尝试编写脚本,删除跨多台计算机的“NT AUTHORITY\Authenticated Users”组的特定文件夹(或文件)的修改权限(实际上是文件部署脚本的一部分)。我有一些代码尝试这样做,但它没有按预期工作。

上下文:

脚本在本地复制文件结构,并且需要将特定文件(理想情况下是整个文件夹结构)设为不可修改(由非管理员)。仅设置只读设置是不够的,因为具有文件修改权限的人可以将其还原。

我的尝试:

$folder = "C:\folder"
$file = "C:\folder\file.ext"

# Get the existing ACL
$acl = Get-Acl -Path $folder

# See what it looks like
$acl.Access | ft

# Target and remove the specific modify rule
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("NT AUTHORITY\Authenticated Users","Modify","Allow")
$acl.RemoveAccessRule($rule)

# Check that the modification to the PS object took
$acl.Access | ft

# Perform the modification
$acl | Set-Acl -Path $folder

# Check that the modification to the folder/file took
$acl = Get-Acl -Path $folder
$acl.Access | ft

我的结果:

.RemoveAccessRule() 调用对 ACL 没有影响(即使它返回 True),如第二个 $acl.Access | ft 所示。因此,Set-Acl 调用也无效。我怀疑也许我没有正确定位我想要的规则,也许.RemoveAccessRule() 调用返回True 只是因为技术上没有“故障”。但这只是在黑暗中的一个镜头。

这是$acl.Access | ft 在所有情况下的输出:

           FileSystemRights AccessControlType IdentityReference                IsInherited InheritanceFlags PropagationFlags
           ---------------- ----------------- -----------------                ----------- ---------------- ----------------
                FullControl             Allow BUILTIN\Administrators                  True             None             None
                FullControl             Allow NT AUTHORITY\SYSTEM                     True             None             None
        Modify, Synchronize             Allow NT AUTHORITY\Authenticated Users        True             None             None
ReadAndExecute, Synchronize             Allow BUILTIN\Users                           True             None             None

我就是无法摆脱 Modify 标志。我也尝试过直接定位文件(在上面的代码中用$file 替换对$folder 的引用),但结果是一样的。我也试过用Authenticated Users 替换NT AUTHORITY\Authenticated Users,但没有效果。

问题:

大概是我做错了。我精通 Powershell,但以前没有使用它配置 NTFS 权限的经验。也许我需要以不同的方式定位所需的规则,或者我需要用 .SetAccessRule() 覆盖它而不是以某种方式......

如何使用 Powershell 实现这一目标?感谢您的宝贵时间。

更新:也许继承也是一个问题?我不确定如何处理。

来源:

我的环境:

  • Windows 10 x64 20H2
  • Powershell 5.1
  • 在我的测试中,我以具有本地管理员权限的用户身份在提升的 Powershell 控制台中运行代码。实际上,代码将由 MECM 在复制文件的同一脚本中运行(可能是 SYSTEM 或类似的)。

【问题讨论】:

  • 更新:我已经解决了。继承确实是个问题。我会尽快发布完整解决方案的答案。

标签: windows powershell acl


【解决方案1】:

原来我的问题是该文件夹从C:\ 继承了修改权限。显然,在启用继承时修改权限根本不会执行任何操作并且不会引发错误 (>:/)。解决方案是先禁用文件夹的继承(复制现有权限),然后其余代码按预期工作。

就我而言,完全删除NT AUTHORITY\Authenticated Users 的所有权限就足够了(并且比对现有权限进行修改更容易),因为BUILTIN\Users 仍然具有读取权限。

# Get the existing ACL
$acl = Get-Acl -Path $item

# Disable inheritance (copying permissions), so the modifications will actually take
$acl.SetAccessRuleProtection($true,$true)

# Perform the modification
$acl | Set-Acl -Path $item

# Get the existing ACL again
$acl = Get-Acl -Path $item

# Target and remove all permission for "NT AUTHORITY\Authenticated Users"
$rules = $acl.Access | Where { $_.IdentityReference -eq $identity }
foreach($rule in $rules) {
    $acl.RemoveAccessRule($rule)
}

# Perform the modification
$acl | Set-Acl -Path $item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多