CajunTechie
CajunTechie

Reputation: 605

Setting Windows XP registry key permissions using Python

A client of mine hosed part of their registry. For some reason, a bunch of sub keys under the HKEY_CLASSES_ROOT have no permissions set. So I am going through the keys and manually setting keys as such:

  1. Add Administrators as a group
  2. Set Administrators as the Owner

There are potentially thousands of these that need to be set and it's a 10-12 step process to do for each key. So I want to automate the process via Python. Is there a module that can accomplish both of these?

Thanks!

Upvotes: 0

Views: 813

Answers (1)

oz123
oz123

Reputation: 28848

After almost a whole day research my solution to working with windows registry and permissions is to use SetACL. You could use a COM object, or use the binary file and the subprocess module. Here is a snippet from what I used in my code to modify the permissions in a mixed environment (I have ~50 Windows machines with 32bit and 64bit, with Windows 7 and Windows XP pro ...):

from subprocess import Popen, PIPE

def Is64Windows():
    '''check if win64 bit'''
    return 'PROGRAMFILES(X86)' in os.environ

def ModifyPermissions():
    """do the actual key permission change using SetACL"""
    permissionCommand = r'SetACL.exe -on "HKLM\Software\MPICH\SMPD"'\
    +' -ot reg -actn ace -ace "n:Users;p:full"'
    permissionsOut = Popen(permissionCommand, stdout = PIPE, stderr = PIPE)
    pout, perr = permissionsOut.communicate()
    if pout:
        print pout
        sys.exit(0)
    elif perr:
        print perr
        sys.exit(1)

def main():
    ... some code snipped ...

    os.chdir('SetACL')
    if Is64Windows():
        os.chdir('x64')
        ModifyPermissions()
    else:
        os.chdir('x86')
        ModifyPermissions()

So, it's not really pure Python, but it works.

Upvotes: 1

Related Questions