VBScript that will query registry permissions

Posted on 2009-02-13
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
    LVL 65

    Accepted Solution

    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

    LVL 2

    Author Closing Comment

    Many thanks for your help with this.


    LVL 65

    Expert Comment

    No problem. Thanks for the grade.



    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
    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…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now