Error trying to use wmi objects (python)

I am trying to write a script that determines whether the machine on which the script is running, a virtual machine or a physical machine, and I do not understand the error and how to fix it.

import wmi

def sys_info():

    objWMIService = wmi.GetObject("winmgmts:\root\cimv2")
    colItems = objWMIService.ExecQuery("Select * from Win32_BaseBoard")

    for objItem in colItems:
        print "inside"
        Manufacturer = objItem.Manufacturer
        if Manufacturer == "Microsoft Corporation":
            print "Virtual Machine"
        else:
            print "Not in one"

Error:

    Traceback (most recent call last):
  File "C:\Documents and Settings\xxx\Desktop\Python\Practice Code\System information\trial.py", line 16, in <module>
    sys_info()
  File "C:\Documents and Settings\xxx\Desktop\Python\Practice Code\System information\trial.py", line 5, in sys_info
    objWMIService = wmi.GetObject("winmgmts:""\root\cimv2")
  File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 72, in GetObject
    return Moniker(Pathname, clsctx)
  File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 87, in Moniker
    moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147217375, 'OLE error 0x80041021', None, None)

I hope someone can help, I'm pretty new to python. Thank.

+3
source share
1 answer

Something will improve if you change:

objWMIService = wmi.GetObject("winmgmts:\root\cimv2") 

to

objWMIService = wmi.GetObject(r"winmgmts:\root\cimv2") 

'\ r' '\ root' <CR> . "\", , , "r" ( ), Python, "" , , Python .

+5

Source: https://habr.com/ru/post/1796044/


All Articles