[Tutor] Com & Python

David David@snowcrash.co.uk
Tue, 28 May 2002 09:21:31 +0100


Looking for any pointers on working with some com objects!

Specifically
I am writing a script which manages a com object developed by citrix to
allow management of a citrix metaframe farm!

Problem is I have code in python which can do basic things with the
object but I'm having problems when it returns a collection object I
want to iterate through...can seem to get anywhere; bummer is I have
working code in vbscript but that lets me down in other areas hence why
I wanted to use python...


Python excerpt:

MetaFrameWinFarmObject = 1
MetaFrameWinAppObject = 3
MFWinAppDesktop = 8

#this is test for citrix com stuff
objFarm = win32com.client.Dispatch("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(MetaFrameWinFarmObject)
if objFarm.WinFarmObject.IsCitrixAdministrator == 0: print "not a citrix
admin"
objApplication = objFarm.GetApplication(MetaFrameWinAppObject,
"Applications\Notepad")
print "before load data"
print objApplication.EnableApp
objApplication.LoadData(1) #this is required to load data to this object
print "after load data "
print objApplication.EnableApp
print " now try changing things"
objApplication.EnableApp=1
objApplication.SaveData() #this is required to commit changes to this
object
#print new value
print objApplication.EnableApp
#works ok up to here - sets the above values correctly
#At this point can't work out how to get data out of this collections
object!!!!!...
???  objApplication.Sessions  ????
Have tried just printing it doing 
for x in objApplication.Sessions
And just get error's


Just wondering if I'm being really thick and missing some obvious about
working with com collections?

Working vscript
(much of this taken from citrix sdk)
		Dim theFarm2 		'this is a citrix farm object
used to kill users
		Dim bApplication	'this is a citrix app object
used to kill users
		Dim aSession	'individual session within the
collection for user logoff
		Dim bSession	'individual session within the
collection for user message

		' Create MetaFrameFarm object
		Set theFarm2 =
CreateObject("MetaFrameCOM.MetaFrameFarm")
		if Err.Number<> 0 Then
		' Display the Error that occured
		'	WScript.Echo "Can't create MetaFrameFarm object
when getting status"
		'	WScript.Echo "(" & Err.Number & ") " &
Err.Description
		'	WScript.Echo ""
			WScript.Quit Err.Number
		End if

		' Initialize the farm object.
		theFarm2.Initialize(MetaFrameWinFarmObject)
		if Err.Number<> 0 Then
		' Display the Error that occured
		'	WScript.Echo "Can't  Initialize MetaFrameFarm
object when getting status"
		'	WScript.Echo "(" & Err.Number & ") " &
Err.Description
		'	WScript.Echo ""
			WScript.Quit Err.Number
		End if

		' Are you Citrix Administrator?
		if theFarm2.WinFarmObject.IsCitrixAdministrator = 0 then
		'	WScript.Echo _
		'	"You must be a Citrix administrator to run this
application when getting status" 
		'	WScript.Echo ""
			WScript.Quit 0
		End If

		' Print out the farm name.
		'WScript.Echo "MetaFrame Farm Name (when getting
status): " & theFarm2.FarmName
		' MetaFrameApplication object.
		Set bApplication =
theFarm2.GetApplication(MetaFrameWinAppObject, strAppDN)
		if Err.Number<> 0 Then
		' Display the Error that occured
		'	WScript.Echo "Can't get application object when
getting status"
		'	WScript.Echo "(" & Err.Number & ") " &
Err.Description
		'	WScript.Echo ""
			WScript.Quit Err.Number
		End if 

		'refresh data for the application object
		bApplication.LoadData(TRUE)
	
		'Send user logoff warning
		For Each bSession In bApplication.Sessions
			if Err.Number<> 0 Then
			' Display the Error that occured
		'		WScript.Echo "Can't enumerate sessions
to message users"
		'       	WScript.Echo "(" & Err.Number & ") " &
Err.Description
		'	        WScript.Echo ""
			        WScript.Quit Err.Number
			End if  
			'logoff user without waiting for the disconnect
to finish
			bSession.sendMessage _
			bSession.ServerName, bSession.SessionID, _
			bSession.UserName, bSession.ClientName, _
			"Point of Sale", _
			strWarningTxt, _
			0, _
			5000, _
			0
		Next
		'refresh data for the application object
		bApplication.LoadData(TRUE)
	
		'Kill All Users
		For Each aSession In bApplication.Sessions
			if Err.Number<> 0 Then
			' Display the Error that occured
		'		WScript.Echo "Can't enumerate sessions
to kill users"
		'		WScript.Echo "(" & Err.Number & ") " &
Err.Description
		'	        WScript.Echo ""
	        		WScript.Quit Err.Number
			End if  
		'	WScript.Echo "ServerName: " &
aSession.ServerName &_
		'		", SessionName: " & aSession.SessionName
&_
		'		", SessionID: " &
CStr(aSession.SessionID) &_
		'		", User: " & aSession.UserName &_
		'		", Client Name: " & aSession.ClientName
			'logoff user without waiting for the disconnect
to finish
			aSession.logoff false
		Next