vanClacc
vanClacc

Reputation: 33

Registry key changes with Python winreg not taking effect, but not throwing errors

Still really new to Python and I'm trying to write a script that will allow me to change specific registry keys on a remote machine and I'm having some trouble with it. Basically the code I have runs with no errors, but the key value is also not set. I'm running it from Windows command prompt as Administrator, using an account that has admin rights on the target machine. Here's the relevant code:

registry = winreg.ConnectRegistry(fullSysName, winreg.HKEY_LOCAL_MACHINE)
wholeKey = winreg.OpenKey(registry, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", 0, winreg.KEY_ALL_ACCESS)

print('Setting AutoAdminLogon\n')
winreg.SetValue(wholeKey, 'AutoAdminLogon', winreg.REG_SZ, '1')
winreg.CloseKey(wholeKey)


winreg.CloseKey(registry)

fullSysName is a variable that contains the target machine name derived from earlier in the script. The script runs with no errors, and I have admin rights, so I am at a loss as to why it's not working. Ended up with some Powershell to do it, but it bothers me that I can't get this to work and would at least like to understand why. I've confirmed that it doesn't have an effect even when I manually replace 'fullSysName' with the machine name. Appreciate any tips you're able to provide!

Upvotes: 3

Views: 2314

Answers (1)

Daniel Zaksevski
Daniel Zaksevski

Reputation: 68

Okay so basically there are two things you need to do: First one is, when you open a key and give access to your program, you need to be more specific, meaning that you need to specify if your machine is 32-bit or 64-bit. So, for example, my machine is 64-bit so I need to change my key opening to this:

wholeKey = winreg.OpenKey(registry, 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon', 0, winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_64KEY)

For a 32-bit machine you would need to add winreg.KEY_WOW64_32KEY

The second thing is, SetValue does not always work, so you need to use SetValueEx which takes 5 arguments (the added argument must be 0). So in your case:

winreg.SetValueEx(wholeKey, 'AutoAdminLogon', 0, winreg.REG_SZ, "1")

You can read more about this in the documentation.

Upvotes: 3

Related Questions