简体   繁体   English

使用 Set-Acl 和 FileSystemAccessRule.RemoveAccessRule 方法不起作用

[英]Using Set-Acl and the FileSystemAccessRule.RemoveAccessRule method not working

Summary:概括:

I'm trying to script removing the modify permission of a particular folder (or file) for the "NT AUTHORITY\Authenticated Users" group, across multiple machines (actually as part of a file deployment script).我正在尝试编写脚本删除“NT AUTHORITY\Authenticated Users”组的特定文件夹(或文件)的修改权限,跨多台机器(实际上是文件部署脚本的一部分)。 I have some code that attempts to do this, but it is not working as expected.我有一些代码试图这样做,但它没有按预期工作。

Context:语境:

The script copies down a file structure locally, and a particular file (ideally the whole folder structure) needs to be made unmodifiable (by non-admins).该脚本在本地复制文件结构,并且需要使特定文件(理想情况下是整个文件夹结构)不可修改(由非管理员)。 Setting the read-only setting isn't sufficient, since it can be reverted by those with modify permissions to the file.设置只读设置是不够的,因为具有文件修改权限的人可以将其还原。

My attempt:我的尝试:

$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

My results:我的结果:

The .RemoveAccessRule() call has no effect on the ACL (even though it returns True ), as shown by the second $acl.Access | ft .RemoveAccessRule()调用对 ACL 没有影响(即使它返回True ),如第二个$acl.Access | ft所示。 $acl.Access | ft . $acl.Access | ft As such, the Set-Acl call has no effect either.因此, Set-Acl调用也无效。 I suspect that perhaps I'm not targeting the rule I want correctly, and perhaps the .RemoveAccessRule() call is returning True just because there were technically no "failures".我怀疑也许我没有正确定位我想要的规则,也许.RemoveAccessRule()调用返回True只是因为技术上没有“失败”。 But that's just a shot in the dark.但这只是在黑暗中的一个镜头。

Here is what the output of $acl.Access | ft这是$acl.Access | ft的 output 的内容。 $acl.Access | ft looks like in all cases: $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

I just can't get rid of that Modify flag.我就是无法摆脱那个Modify标志。 I've also tried targeting the file directly (replacing references to $folder with $file in the above code), but the result is the same.我也尝试过直接定位文件(在上面的代码中用$file替换对$folder的引用),但结果是一样的。 I've also tried replacing NT AUTHORITY\Authenticated Users with just Authenticated Users , to no effect.我也尝试用 Authenticated Users 替换NT AUTHORITY\Authenticated Users Authenticated Users ,但没有效果。

Question:问题:

Presumably I'm just doing it wrong.大概我只是做错了。 I'm fluent in Powershell, but have no previous experience using it to configure NTFS permissions.我精通 Powershell,但之前没有使用它配置 NTFS 权限的经验。 Perhaps I need to target the desired rule differently, or perhaps I need to overwrite it with .SetAccessRule() instead somehow...也许我需要以不同的方式定位所需的规则,或者我需要用.SetAccessRule()以某种方式覆盖它......

How can I achieve this with Powershell?如何使用 Powershell 实现这一目标? Thanks for your time.谢谢你的时间。

Update: also perhaps inheritance is an issue?更新:也许 inheritance 是个问题? I'm unsure how to deal with that.我不确定如何处理。

Sources:资料来源:

My environment:我的环境:

  • Windows 10 x64 20H2 Windows 10 x64 20H2
  • Powershell 5.1 Powershell 5.1
  • In my testing, I'm running the code in an elevated Powershell console, as a user with local admin permissions.在我的测试中,我以具有本地管理员权限的用户身份在提升的 Powershell 控制台中运行代码。 In practice the code will be run by MECM in the same script that copies the files down (presumably as SYSTEM or some such).在实践中,代码将由 MECM 在复制文件的同一脚本中运行(可能是 SYSTEM 或类似的)。

Turns out my problem was that the folder was inheriting the modify permission from C:\ .原来我的问题是该文件夹继承了C:\的修改权限。 Apparently modifying permissions when inheritance is enabled simply does nothing and throws no errors (>:/).显然,在启用 inheritance 时修改权限根本不会执行任何操作并且不会引发错误 (>:/)。 The solution was to disable inheritance on the folder first (copying the existing permissions), and then the rest of the code works as expected.解决方案是先禁用文件夹上的 inheritance(复制现有权限),然后代码的 rest 按预期工作。

In my case, simply removing all permissions for NT AUTHORITY\Authenticated Users entirely was sufficient (and easier than performing modifications on the existing permissions), since BUILTIN\Users still has read permissions.就我而言,完全删除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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM