0

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!

CajunTechie
  • 605
  • 1
  • 8
  • 21
  • 2
    take a look at http://docs.python.org/library/_winreg.html – Joran Beasley Mar 17 '12 at 21:02
  • @JoranBeasley, the _winreg module is poorly documented. And so is in General the situtation regarding Windows Registry. The people in Redmond created a beast they don't even fully understand... – oz123 May 16 '12 at 11:44

1 Answers1

1

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.

oz123
  • 27,559
  • 27
  • 125
  • 187