Problem listing services with wmi

Tim Golden tim.golden at viacom-outdoor.co.uk
Tue May 17 12:19:28 EDT 2005


[... snip results ...]

| So it would seem that the 3 methods give the same result. As to which 
| service it has gotten to when it gets to position 88 in the list, 
| obviously I can't find out with a script, and it seems that the list 
| isn't in any order I can see, so I couldn't even venture a 
| guess... I'll 
| try looking if there's a service in my list that isn't valid 
| (points to 
| an uninstalled program for example) or something like that... 
| But if you 
| have other ideas please let me know.

Well I honestly don't know if this will go any further,
but the following code uses the win32service module from
pywin32 to collect the service information, on the off-chance
that it *won't* fail where WMI does. I can't easily test it
since the script doesn't raise an error on my machine.

At the end, it tries to print out the differences between
the win32-collected info and the wmi-collected info.

<code>

#
# Not sure if you're running 2.3/2.4
#
try:
  set
except NameError:
  from sets import Set as set

##
## Taken from a post to c.l.py by William J. Rice
##
import win32service
import win32con

accessSCM = win32con.GENERIC_READ
hscm = win32service.OpenSCManager (None, None, accessSCM)

win32_names = set ()

for service_info in win32service.EnumServicesStatus(hscm):
  win32_names.add (service_info[1])

import win32com.client

c = win32com.client.GetObject (
  "winmgmts:{impersonationLevel=Impersonate,authenticationLevel=Default}/root/cimv2"
)

wmi_names = set ()
for service in c.ExecQuery ("SELECT * FROM Win32_Service"):
  try:
    short_name = service.Properties_ ("Caption").Value
  except:
    pass
  else:
    wmi_names.add (short_name)

print "only in win32:", ", ".join (win32_names - wmi_names)
print

print "only in wmi:", ", ".join (wmi_names - win32_names)
print

</code>

Let me know if anything shows up.

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list