Read a single registry key value on a remote machine

5.2k Views Asked by At

I am having a very tough time achieving this one seemingly very simple goal...

I have to gather the value of a single registry key on several machines for the sake of auditing whether the machines scanned need to be patched with newer versions of software. I am only permitted to use python 3 as per our company policy (which is on drugs, but what can i do).

i have been looking into using the winreg module to connect to the remote machine (using credentials, we are on a domain) but I am confronted time and again with

TypeError: The object is not a PyHKEY object (or a number of other issues.)

this seems like a very common need and i've been surprised at the difficulty i have had finding any examples for python 3 that i can use to figure out what I'm doing wrong.

Any assistance that anyone would be kind enough to give would be greatly appreciated. Thanks in advance.

2

There are 2 best solutions below

1
On

Can you show the code which you are writing? Have you opened the key? Many people do get problems since they have not opened it? This is just a guess, hope it works

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Enum\Root')
0
On

The winreg module doesn't allow you to do what reg query does. So for example to read the BuildLabEx reg key value here's what I do:

import subprocess

keyPath = "\\\\RemoteMachineName\\HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion"
output = subprocess.run(["reg", 
                 "query",
                 keyPath,
                 "/v",
                 "BuildLabEx"], 
               capture_output=True,
               text=True)
print(output.stdout)

The above snippet is equivalent to:

reg query "\\RemoteMachineName\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion" /v BuildLabEx