Reputation:
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?
Upvotes: 2
Views: 6471
Reputation: 402
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)
Upvotes: 1
Reputation: 31
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
Upvotes: 3
Reputation:
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
Upvotes: 0
Reputation: 391818
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)
Upvotes: 0