tjguk / wmi

108 stars 29 forks source link

List Namespaces fails without computer specified #2

Open tjguk opened 8 years ago

tjguk commented 8 years ago

[from werner.haas@FireEye.com via email]

I just got started so I loved your examples from the “wmi Cookbook” but unfortunately the “List Namespaces” script failed right away with com_error=(-2147217406, 'OLE error 0x80041002', None, None). Either I add computer=”.”, or I have to select one of root’s namespaces to start the recursion.

g40 commented 7 months ago

Hi and thanks. Very useful.

Below is an extremely modest update for Python 3, which gives me paths like this:

root/Microsoft/Windows/Storage
root/Microsoft/Windows/Storage/PT
root/Microsoft/Windows/Storage/PT/Alt
root/Microsoft/Windows/Storage/MS_809
root/Microsoft/Windows/Storage/MS_409
root/Microsoft/Windows/Storage/Providers_v2
root/Microsoft/Windows/HardwareManagement
root/Microsoft/Windows/HardwareManagement/MS_809
root/Microsoft/Windows/HardwareManagement/MS_409
# python 3.12 here.
def enumerate_namespaces(namespace=u"root",level=0):
    try:
        # print the whole namespace for clarity
        print(f"{namespace}")
        # computer keyword must be used
        ns = wmi.WMI(namespace=namespace,computer=".")
        for subnamespace in ns.__NAMESPACE():
            enumerate_namespaces(namespace + "/" + subnamespace.Name, level + 1)
    except:
        pass

enumerate_namespaces()