VBScript that will query registry permissions

Posted on 2009-02-13
Medium Priority
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.


Question by:aideb
  • 2
LVL 65

Accepted Solution

RobSampson earned 2000 total points
ID: 23646334
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.


' 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
    'Wscript.Echo "No Query Value Access Rights on HKEY_LOCAL_MACHINE\" & strKeyPath
	'Delete new key
	intReturn = objReg.DeleteKey(HKEY_LOCAL_MACHINE, strKeyPath)
	If (intReturn = 0) And (Err.Number = 0) Then    
		'Wscript.Echo "HKEY_LOCAL_MACHINE\" & strKeyPath & " successfully deleted."
		'Wscript.Echo "DeleteKey failed. Error = " & Err.Number
	End If
End If

Open in new window


Author Closing Comment

ID: 31546522
Many thanks for your help with this.


LVL 65

Expert Comment

ID: 23716428
No problem. Thanks for the grade.



Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question