Reputation: 1017
I am trying to get some system information from Pythons WMI module however allot of the methods mention online don't seem to be working. I know it is easier to do it in other languages but I want to try and do this in Python for reasons.
I have tried using the Win32_TemperatureProbe()
method but I get a return value of None
. I have also tried the MSAcpi_ThermalZoneTemperature()
and the error for that is below. I have been doing my research and can't seem to find any alternative methods or fixes to these errors. There seem to be allot of threads open but none solved.
Here is my current code:
import wmi, platform
#Initiating WMI
c = wmi.WMI(namespace="root\\wmi")
#Setting variables
drive_letter, free_space = [], []
stats = {}
#Getting basic infromation
print(platform.processor())
print(c.MSAcpi_ThermalZoneTemperature()[0].CurrentTemperature/10.0)-273.15
for disk in c.Win32_LogicalDisk(["Caption","FreeSpace"], DriveType=3):
free_space_GB = round(int((disk.FreeSpace))/1073741824, 2)
drive_letter.append(str(disk.Caption))
free_space.append(str(free_space_GB))
stats = {"Free Space":free_space, "Drive Letter":drive_letter}
print(stats)
and here is the output I get when running this code:
Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\wmi.py", line 817, in query
return self._namespace.query (wql, self, fields)
File "C:\Python34\lib\site-packages\wmi.py", line 1009, in query
return [ _wmi_object (obj, instance_of, fields) for obj in self._raw_query(wql) ]
File "C:\Python34\lib\site-packages\wmi.py", line 1009, in <listcomp>
return [ _wmi_object (obj, instance_of, fields) for obj in self._raw_query(wql) ]
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 247, in __getitem__
return self._get_good_object_(self._enum_.__getitem__(index))
File "C:\Python34\lib\site-packages\win32com\client\util.py", line 37, in __getitem__
return self.__GetIndex(index)
File "C:\Python34\lib\site-packages\win32com\client\util.py", line 53, in __GetIndex
result = self._oleobj_.Next(1)
pywintypes.com_error: (-2147217396, 'OLE error 0x8004100c', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Brandyn\Desktop\Performance_monitor.py", line 12, in <module>
print(c.MSAcpi_ThermalZoneTemperature()[0].CurrentTemperature/10.0)-273.15
File "C:\Python34\lib\site-packages\wmi.py", line 819, in query
handle_com_error ()
File "C:\Python34\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217396, 'OLE error 0x8004100c', None, None)>
When I change the namespace to null, the error changes to this:
Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\wmi.py", line 1145, in __getattr__
return self._cached_classes (attribute)
File "C:\Python34\lib\site-packages\wmi.py", line 1156, in _cached_classes
self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
File "<COMObject winmgmts:>", line 3, in Get
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 282, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemServicesEx', 'Not found ', None, 0, -2147217406), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Brandyn\Desktop\Performance_monitor.py", line 12, in <module>
print(c.MSAcpi_ThermalZoneTemperature()[0].CurrentTemperature/10.0)-273.15
File "C:\Python34\lib\site-packages\wmi.py", line 1147, in __getattr__
return getattr (self._namespace, attribute)
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: winmgmts:.MSAcpi_ThermalZoneTemperature
Is there a fix to this method or is there another viable alternative or should I just simply stop this and focus on a different language?
Upvotes: 2
Views: 3624
Reputation: 104
I had the same issue.
For Win32_LogicalDisk
, you need to replace namespace root\\wmi
with root\\cimv2
.
Then it worked for me on Win7.
For MSAcpi_ThermalZoneTemperature
, I have not found a solution. The object looks as follows
>>> print c.MSAcpi_ThermalZoneTemperature
[dynamic: ToInstance, provider("WMIProv"), WMI, guid("{A1BC18C0-A7C8-11d1-BF3C-00A0C9062910}")]
class MSAcpi_ThermalZoneTemperature : MSAcpi
{
[key, read] string InstanceName;
[read] boolean Active;
[WmiDataId(1), read] uint32 ThermalStamp;
[WmiDataId(2), read] uint32 ThermalConstant1;
[WmiDataId(3), read] uint32 ThermalConstant2;
[WmiDataId(4), read] uint32 Reserved;
[WmiDataId(5), read] uint32 SamplingPeriod;
[WmiDataId(6), read] uint32 CurrentTemperature;
[WmiDataId(7), read] uint32 PassiveTripPoint;
[WmiDataId(8), read] uint32 CriticalTripPoint;
[WmiDataId(9), read] uint32 ActiveTripPointCount;
[WmiDataId(10), MissingValue(0), read, MAX(10)] uint32 ActiveTripPoint[];
};
BUT c.MSAcpi_ThermalZoneTemperature.CurrentTemperature
returns None
.
Interesting is if we run folliwing code
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\WMI")
colItems = objSWbemServices.ExecQuery("SELECT * FROM MSAcpi_ThermalZoneTemperature")
print repr(colItems)
print len(colItems)
then calling print repr(colItems)
returns <COMObject <unknown>>
and calling print len(colItems)
generates an exception:
Traceback (most recent call last):
File "get_wmi_test1.py", line 45, in <module>
print len(colItems)
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 228, in
__len__
return self._oleobj_.Invoke(dispid, LCID, invkind, 1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'SWbemObjectSet'
, u'Not supported ', None, 0, -2147217396), None)
I looked a bit around and seems to be more complex issue. Look here:
Upvotes: 1