[python-win32] wmi query.
Tim Golden
mail at timgolden.me.uk
Mon Mar 24 21:52:12 CET 2008
mark.a.brand wrote:
> hi:
>
> how do you incorporate a where clause and selected field clause in a wmi
> query ?
>
> this gets me caption and state fields for all services
> c = wmi.WMI()
> for service in c.Win32_Service (['Caption', 'State'])
> ...
>
> this gets me all fields for stopped services
> c = wmi.WMI()
> for service in c.Win32_Service ('State == 'Stopped')
> ....
>
> is the way to incorporate both queries into one something like this
> c = wmi.WMI()
> for service in c.Win32_Service (['Caption', 'State']) WHERE ('State ==
> 'Stopped')
Well, I can answer this a couple of ways:
1) You can always pass raw WQL to the namespace (the result
of calling wmi.WMI () - in your case, c). So:
<code>
import wmi
c = wmi.WMI ()
Q = "SELECT caption FROM Win32_Service WHERE state = 'Stopped'"
for service in c.query (Q):
print service.caption
</code>
but...
2) ...you'd only really need to do this is you were doing
something sophisticated with the where clause. Normally you'd
write it like this:
<code>
import wmi
c = wmi.WMI ()
for service in c.Win32_Service (['caption'], state="Stopped"):
print service.caption
</code>
Even that is a slight optimisation. It usually costs little
to pull back all the fields, so I generally write:
<code>
import wmi
c = wmi.WMI ()
for service in c.Win32_Service (state="Stopped"):
print c.Caption, c.Status
</code>
TJG
More information about the python-win32
mailing list