We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

VBScript that will query registry permissions

aideb
aideb asked
on
Medium Priority
1,160 Views
Last Modified: 2012-05-06
We have a bad application in our environment which has broken the permissions on a registry key.

I am looking to create a VBScript that will check if 'SYSTEM' has permissions on a registry key HKLM\Software\Application\Component

If SYSTEM does not have permission on the key then I want it to delete from HKLM\Software\Application down as the application will be reinstalled and it will repopulate the key with the correct permissions. As the permissions have been stripped out, the installation will not work.

Thanks

Aidan
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hi, if you run this script as a StartUp script, it will run under the local SYSTEM account, and will therefore be able to check it's own permissions.  This should remove the specified reg key if it does not have access.

Regards,

Rob.
' Create constants for access rights and registry hive 
Const KEY_QUERY_VALUE = &H0001
Const HKEY_LOCAL_MACHINE = &H80000002
 
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "Software\Application\Component"
 
On Error Resume Next
' Does the account under which the script runs have the 
'    right to query the SYSTEM\CurrentControlSet key
objReg.CheckAccess HKEY_LOCAL_MACHINE, strKeyPath, KEY_QUERY_VALUE, bHasAccessRight
If bHasAccessRight = True Then
	'Wscript.Echo "Has Query Value Access Rights on HKEY_LOCAL_MACHINE\" & strKeyPath
Else
    'Wscript.Echo "No Query Value Access Rights on HKEY_LOCAL_MACHINE\" & strKeyPath
	'Delete new key
	Err.Clear
	intReturn = objReg.DeleteKey(HKEY_LOCAL_MACHINE, strKeyPath)
	If (intReturn = 0) And (Err.Number = 0) Then    
		'Wscript.Echo "HKEY_LOCAL_MACHINE\" & strKeyPath & " successfully deleted."
	Else
		'Wscript.Echo "DeleteKey failed. Error = " & Err.Number
	End If
End If

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Many thanks for your help with this.

Cheers

Aidan
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
No problem. Thanks for the grade.

Regards,

Rob.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.