设置 ACL 文件夹权限 - 拒绝系统

Setting ACL Folder Permissions - Deny to System

我已经使用了以前脚本中的一些工作代码,这些代码使用 POSH 更改文件夹的安全权限,但是,我现在不想允许修改帐户的权限,而是想拒绝 SYSTEM 访问此文件夹. ({这是为了在 MDT 任务序列期间关闭来自 运行ning 的可怕 Windows10UpgraderApp.exe})。

这是我认为可行的代码:

New-Item -ItemType directory -Path C:\Windows10upgrade -force

$privuser = "system"
$Acl1 = Get-Acl "C:\Windows10Upgrade"
$Ar1 = New-Object system.security.accesscontrol.filesystemaccessrule("$privuser","deny")
$Acl1.SetAccessRule($Ar1)

这是我遇到的错误:

New-Object : Cannot find an overload for "FileSystemAccessRule" and the argument count: "2".
At line:3 char:8
+ $Ar1 = New-Object system.security.accesscontrol.filesystemaccessrule("$privuser" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

这个有点丢失 - 此代码适用于以前使用的脚本。

提前致谢

编辑:收到以下建议后(谢谢)我现在 运行 此代码:

New-Item -type directory -path C:\Windows10Upgrade -force
$Acl = Get-Acl "C:\Windows10Upgrade"
$Ar = New-Object  system.security.accesscontrol.filesystemaccessrule("System","FullControl","Deny")
$Acl.SetAccessRule($Ar)
Set-Acl "C:\Windows10Upgrade" $Acl

我知道这可能具有欺骗性,因为当您以这种方式进行更改时会勾选“特殊权限”框,但是,这不会引发错误,但系统的权限看起来是一样的,很遗憾,它没有成功应用对访问规则的更改。

第二次编辑:似乎发生了一些事情,但现在有多个 SYSTEM 权限条目,所需的 ACL 规则在 SYSTEM 的顶部,但是,完全控制仍然分配在下面:

PS C:\WINDOWS\system32> get-acl -Path C:\Windows10upgrade | select *


PSPath                  : Microsoft.PowerShell.Core\FileSystem::C:\Windows10upgrade
PSParentPath            : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName             : Windows10upgrade
PSDrive                 : C
PSProvider              : Microsoft.PowerShell.Core\FileSystem
CentralAccessPolicyId   : 
CentralAccessPolicyName : 
AccessToString          : NT AUTHORITY\SYSTEM Deny  FullControl
                          BUILTIN\Administrators Allow  FullControl
                          BUILTIN\Administrators Allow  268435456
                          NT AUTHORITY\SYSTEM Allow  FullControl
                          NT AUTHORITY\SYSTEM Allow  268435456
                          BUILTIN\Users Allow  ReadAndExecute, Synchronize
                          NT AUTHORITY\Authenticated Users Allow  Modify, Synchronize
                          NT AUTHORITY\Authenticated Users Allow  -536805376
AuditToString           : 
Path                    : Microsoft.PowerShell.Core\FileSystem::C:\Windows10upgrade
Owner                   : BUILTIN\Administrators
Group                   : COMPANY\Roles - Technical Services
Access                  : {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule, 
                          System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule...}
Sddl                    : O:BAG:S-1-5-21-2593231249-3506496172-1181922232-40387D:AI(D;;FA;;;SY)(A;ID;FA;;;BA)(A;OICIIOID;GA;;;BA)(A;ID;FA;;;SY)(A;OI
                          CIIOID;GA;;;SY)(A;OICIID;0x1200a9;;;BU)(A;ID;0x1301bf;;;AU)(A;OICIIOID;SDGXGWGR;;;AU)
AccessRightType         : System.Security.AccessControl.FileSystemRights
AccessRuleType          : System.Security.AccessControl.FileSystemAccessRule
AuditRuleType           : System.Security.AccessControl.FileSystemAuditRule
AreAccessRulesProtected : False
AreAuditRulesProtected  : False
AreAccessRulesCanonical : True
AreAuditRulesCanonical  : True

解决方法在错误信息中。引用 the documentation, a minimum of 3 arguments is required. That's for this constructor:

public FileSystemAccessRule(
    string identity,
    FileSystemRights fileSystemRights,
    AccessControlType type
)

您似乎缺少中间参数,FileSystemRights,因为您已将用户指定为 "system" 并且 AccessControlType 被拒绝...但不是哪些权限拒绝。


编辑

右键单击文件夹,然后转到 Properties > Security > Advanced。您应该会看到类似于下图的屏幕。

蓝色突出显示的行是脚本添加的行。黄色突出显示的行是继承的,因此它不是在文件夹上设置的,而是在整个驱动器上设置的。对我来说,这就是为什么会有多个权限行。

这不是真正的问题,因为 Deny 胜过 Allow。要确认,您可以像上面那样为您的用户或 Admin 组设置 Deny 权限,然后尝试访问该文件夹。

如果权限不是继承的,而是应用于文件夹,您将需要使用 RemoveAccessRule 方法,方法与 SetAccessRule 的使用方法类似:

$remove = New-Object  system.security.accesscontrol.filesystemaccessrule("System","FullControl","Allow")
$Acl.RemoveAccessRule($remove)