2

Could someone shed some lights upon querying through wmi and win32.client objects?

Anytime i try to query throught win32.client object i get this error:

Error: '' object has no attribute 'UserName'

though, i know (wmic class "Win32_ComputerSystem", wmiexplorer etc.) the certain attribute belongs to the object i'm trying to query on:

import win32com.client
...
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root/cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_ComputerSystem")
for objItem in colItems:{
    print objItem.UserName    #Error: '' object has no attribute 'UserName'
}
...

And when i run query on wmi object - everything is just fine:

import wmi
...
c = wmi.WMI()
for objItem in c.query(colItems):{
    print objItem.UserName  # this works now
}
...

What is causing this "no attribute" error? Could this be my OS issue? Running winXP pro, version 2002, sp2. Or is this because of python 2.4 version i am working on?

1
  • 1
    Python doesn't use braces '{}' for code blocks. Commented Mar 28, 2009 at 22:10

4 Answers 4

3

To find out about the "keys" and "values" in WMI query result, you can use "Properties_"

from win32com.client import Dispatch, GetObject import win32con
server = Dispatch("WbemScripting.SWbemLocator")
c = server.ConnectServer("localhost", "root\\cimv2") 
p = c.ExecQuery("Select * from Win32_OperatingSystem")

for i in w.p[0].Properties_:
    print i.Name, " = ", i.Value
Sign up to request clarification or add additional context in comments.

1 Comment

of course NameError: name 'w' is not defined and it's not clear the use of this import win32con
1

try to access properties of colItems this way:

for objItem in colItems[o].Properties_:
    print objItem.Name, objItem.Value

in other circumstances you may prefer to use

repr(objItem.Value) 

and

repr(objItem.Value) 

Comments

0

Why are you doing this?

for objItem in colItems:{
    print objItem.UserName    #Error: '' object has no attribute 'UserName'
}

What causes you to think that the resulting columns will be attributes of the resulting object?

Try this to debug the problem.

for objItem in colItems:{
    print dir(objItem)
}

What actual attributes does it have? Perhaps the columns are identified by number? Perhaps you should be doing objItem[0]?


Edit

what caused the change of attributes in class "Win32_ComputerSystem"??

Nothing. Attributes don't change. The class is not what you think it should be.

Here's another debugging aid.

colItems = objSWbemServices.ExecQuery("Select * from Win32_ComputerSystem")
print type(colItems)
print dir(colItems)

4 Comments

thanks for a reply. it's clear i am missing something... I've looked up for a "Win32_ComputerSystem" class in wmiexplorer - it states this class has an attribute of "UserName". So how come resulting columns are NOT the attributes of resulting object???
print dir(objItem):['AssociatorsAsync_', 'Associators_', 'CLSID', 'Clone_', 'CompareTo_', 'DeleteAsync_', 'Delete_', 'ExecMethodAsync_', 'ExecMethod_', 'GetObjectText_', 'InstancesAsync_', 'Instances_', 'PutAsync_', 'Put_', 'ReferencesAsync_', 'References_', 'SpawnDerivedClass_', 'SpawnInstance_'..
what caused the change of attributes in class "Win32_ComputerSystem"??
How come class is different from the specified one in ExecQuery?
0

Okay guys, here is another piece of code (from http://win32com.goermezer.de/content/view/211/189/):

import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Environment")
for objItem in colItems:{
    print "Caption: ", objItem.Caption
    print "Description: ", objItem.Description
    print "Install Date: ", objItem.InstallDate
    print "Name: ", objItem.Name
    print "Status: ", objItem.Status
    print "System Variable: ", objItem.SystemVariable
    print "User Name: ", objItem.UserName
    print "Variable Value: ", objItem.VariableValue
}

And again the same error is being displayed:

Caption: Error: '' object has no attribute 'Caption' 

What is happening? How come class is different from the specified one in ExecQuery? I mean, if one says "Select * from Win32_ComputerSystem", how can it query different class than Win32_ComputerSystem?

By the way, I am running python 2.4 code through spyce 2.0.3 server.

p.s. I found that brackets {} is one way to get python code block interpreted right - otherwise "expected an indented block" error is thrown

Comments

Your Answer

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