Python winreg 的注册表项更改未生效,但不抛出错误 [英] Registry key changes with Python winreg not taking effect, but not throwing errors

查看:114
本文介绍了Python winreg 的注册表项更改未生效,但不抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对 Python 仍然很陌生,我正在尝试编写一个脚本,该脚本将允许我更改远程计算机上的特定注册表项,但我遇到了一些问题.基本上我运行的代码没有错误,但也没有设置键值.我以管理员身份从 Windows 命令提示符运行它,使用在目标计算机上具有管理员权限的帐户.相关代码如下:

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 是一个变量,包含从脚本前面派生的目标机器名称.该脚本运行时没有错误,而且我拥有管理员权限,所以我不知道为什么它不起作用.最终使用了一些 Powershell 来完成它,但让我感到困扰的是我无法让它工作并且至少想了解原因.我已经确认,即使我用机器名称手动替换fullSysName",它也没有效果.感谢您提供的任何提示!

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!

推荐答案

好的,基本上你需要做两件事:第一个是,当您打开一个密钥并允许访问您的程序时,您需要更具体,这意味着您需要指定您的机器是 32 位还是 64 位.因此,例如,我的机器是 64 位,所以我需要将我的密钥打开更改为:

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)

对于 32 位机器,您需要添加 winreg.KEY_WOW64_32KEY

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

第二件事是,SetValue 并不总是有效,因此您需要使用带有 5 个参数的 SetValueEx(添加的参数必须为 0).所以在你的情况下:

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.

这篇关于Python winreg 的注册表项更改未生效,但不抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆