SWbemObjectEx not found

Tim Golden mail at timgolden.me.uk
Thu Dec 27 05:37:19 EST 2007


jmgmail wrote:
> I have a working VBScript and failed to convert it to Python. Can
> someone help?
> 
> ==VBScript:==
> Set locator = CreateObject("WbemScripting.SWbemLocator")
> Set Services = locator.ConnectServer("smsroot1","root/SMS/site_A")
> Set instCollection =
> Services.Get("SMS_Collection.CollectionID='A000D9'")
> 
> 'Create the direct rule.
> Set instDirectRule =
> Services.Get("SMS_CollectionRuleDirect").SpawnInstance_
> instDirectRule.ResourceClassName = "SMS_R_System"
> instDirectRule.ResourceID = 8566
> instDirectRule.RuleName = "MyDirectRule"
> 
> 'Add the direct rule to the collection.
> instCollection.AddMembershipRule instDirectRule
> ==END==

Couple of points here. First off, this is standard WMI stuff
in spite of the slightly awkward way the VBS appears, so if
I may recommend my own WMI module [1] you may find that
easier. Secondly, this will be difficult for anyone to debug
in any depth without having access to the WMI-provided SMS
namespace. But I'll do my best!

> ==Python==
> import sys,os,getopt
> import win32com.client
> 
> locator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
> locator.Security_.impersonationlevel = 3
> Services = locator.ConnectServer("smsroot1","root/SMS/site_A")
> instCollection = Services.Get("SMS_Collection.CollectionID='A000D9'")
> 
> instDirectRule =
> Services.Get("SMS_CollectionRuleDirect").SpawnInstance_()
> instDirectRule.ResourceClassName = "SMS_R_System"
> instDirectRule.ResourceID = 8566
> instDirectRule.RuleName = "MyDirectRule"
> 
> instCollection.AddMembershipRule(instDirectRule)
> ==END==

Almost certainly your problem here is that VBS is doing
some slightly fancy stuff behind the scenes to set up
this method call, which you'll have to do for yourself in
Python. Try replacing that last line with (untested, obviously):

<code-snippet>
AddMembershipRule = instCollection.Methods_ ("AddMembershipRule")
InParameters = AddMembershipRule.InParameters
#
# might be 1-indexed; not sure
#
InParameters[0] = instDirectRule
instCollection.ExecMethod_ ("AddMembershipRule", InParameters)

</code-snippet>

Come back if that doesn't help or if it isn't clear what's going
on.

TJG

[1] http://timgolden.me.uk/python/wmi.html



More information about the Python-list mailing list