如何解决使用 Set-Acl 和 FileSystemAccessRule.RemoveAccessRule 方法不起作用
总结:
我正在尝试为“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
标志。我也试过直接定位文件(在上面的代码中用 $folder
替换对 $file
的引用),但结果是一样的。我也试过用 NT AUTHORITY\Authenticated Users
替换 Authenticated Users
,但没有效果。
问题:
大概是我做错了。我精通 Powershell,但以前没有使用它来配置 NTFS 权限的经验。也许我需要以不同的方式定位所需的规则,或者我需要用 .SetAccessRule()
覆盖它而不是某种方式......
如何使用 Powershell 实现此目的?感谢您抽出宝贵时间。
更新:也许继承也是一个问题?我不确定如何处理。
来源:
- 以下是我一直在使用的示例(在“删除文件或文件夹权限”部分下):https://petri.com/how-to-use-powershell-to-manage-folder-permissions
- 官方 Set-Acl 文档中未提供此用例的示例:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-acl
- 我还查看了 System.Security.AccessControl.FileSystemSecurity 类及其方法的文档,但是方法文档没有提供相关示例:https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesystemsecurity?view=net-5.0#methods
我的环境:
- Windows 10 x64 20H2
- Powershell 5.1
- 在我的测试中,我以具有本地管理员权限的用户身份在提升的 Powershell 控制台中运行代码。在实践中,代码将由 MECM 在复制文件的同一脚本中运行(大概是 SYSTEM 或类似的)。
解决方法
原来我的问题是该文件夹继承了 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。