I am working with a customer that has encountered some issues with a server template they are using in their VMware environment. Through some troubleshooting, I found that the template, when attempting to add server roles, is looking for a package that is not there. This template has been distributed to a number of locations worldwide, so pulling it back and redoing it is not the first option. I found several registry values that can be deleted which fixes the issue, but they are under keys in a protected hive (HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Services\PackageDetect).
It took some doing on a key to which no one but Trusted Installer has access, but I was able to assign permissions temporarily to a local local account, at the PackageDetect level, with propagation, so I could then delete the needed items. This works well:
$sKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackageDetect", [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions) $inheritFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit) $propagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly $acl = $sKey.GetAccessControl() $rule = New-Object System.Security.AccessControl.RegistryAccessRule ("TempAdmin", "FullControl", $inheritFlag, $propagationFlag, "Allow") $acl.SetAccessRule($rule) $sKey.SetAccessControl($acl)
The problem comes in trying to delete the acl. If I manually delete it at the PackageDetect key it removes from all children as expected. If I manually delete the user account, it also deletes the permission as I would expect. But neither option works for me through a script. If I try this:
$acl = Get-Acl 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackageDetect" $inheritFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit) $propagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly $rule = New-Object System.Security.AccessControl.RegistryAccessRule ("TempAdmin", "FullControl", $inheritFlag, $propagationFlag, "Allow") $acl.RemoveAccess($rule)
This returns True, but the permission is not deleted. I am not sure what I am missing.
I also tried deleting the user account altogether (it is just a temp account used for this process) with a Remove-LocalUser, but this does not produce the desired result either. The user account is deleted, but the permission changes to the user’s GUID and is never cleaned up from the acl. When I manually go into Control Panel and delete the user, however, it immediately removes this permission. I am unsure if there is another way of deleting the user via script that would give me this same functionality.
I would appreciate any suggestions, whether through removing the acl (preferred) or deleting the user account so that all permissions are removed.