2

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.

agf
  • 171,228
  • 44
  • 289
  • 238
MadSc13ntist
  • 19,820
  • 8
  • 25
  • 19

2 Answers2

1

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')
Manish Sinha
  • 2,092
  • 2
  • 22
  • 33
  • You have to open the key first, even if you want to delete it. The error message comes from calling a winreg method with an object that is not a valid (i.e. opened) key handle. – Michael Dillon Oct 25 '09 at 17:31
0

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
yasouser
  • 5,113
  • 2
  • 27
  • 41