3

I'm trying to write a script which detects whether the machine that the script is run on is a virtual machine or a physical machine and I don't 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"

The 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'm hoping someone can help, I'm pretty new to python. Thanks.

1 Answer 1

6

Do things improve if you change:

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

to

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

The '\r' sequence in '\root' will be interpreted as a <CR> character in your code. You either have to doubleup the '\'s to escape them so they will be treated as backslashes, or precede the first double quote with 'r' (as I have done), to indicate to Python that this should be a "raw" string literal. Raw strings are no different from regular strings, but the raw string syntax tells the Python compiler to not interpret backslashes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.