I’m working on a PowerShell Script, which is supposed to
- create a directory,
- grant access to the directory for the active user,
- remove access to the directory for a specific group.
This is what I have right now:
$project_dir = "my_dir"
New-Item -Path "$project_dir" -ItemType Directory -Force | Out-Null
$ACL = Get-Acl $project_dir
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:username", "Modify", 'ContainerInherit,ObjectInherit', 'None', "Allow")
$ACL.SetAccessRule($AccessRule)
Set-Acl -AclObject $ACL $project_dir
$ACL = Get-Acl $project_dir
$ACL.SetAccessRuleProtection($true, $true)
Set-Acl -AclObject $ACL $project_dir
$ACL = Get-Acl $project_dir
$AccessRuleToRemove = (Get-Acl $project_dir).Access | Where-Object {$_.IdentityReference -eq "some_name"}
$ACL.RemoveAccessRule($AccessRuleToRemove)
Set-Acl -AclObject $ACL $project_dir
This works apart from the last line where I get this error:
Set-Acl : Dem Prozess fehlt die für diesen Vorgang erforderliche "SeSecurityPrivilege"-Berechtigung. In Zeile:1 Zeichen:1 + Set-Acl -AclObject $ACL $project_dir + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:my_dir:String) [Set-Acl], PrivilegeNotHeldException + FullyQualifiedErrorId : System.Security.AccessControl.PrivilegeNotHeldException,Microsoft.PowerShell.Commands.SetAclCommand
The weird thing is: When I open the folder’s properties in the Windows Explorer (Alt
+ Enter
), I can navigate to Security > Extended and in this menu, I can delete the exact same AccessRule.
Any idea why this is the case and how I can delete this rule using PowerShell?