Displaying Active Sessions

This VBScript example runs a query to find the active sessions on a Forefront TMG computer and then displays the active sessions found. The script includes a single subprocedure, called DisplayActiveSessions.

This example is included as the ActiveSessions.vbs script in the Samples\Admin folder of the Forefront TMG Software Development Kit (SDK).

The following procedure lists the steps used to find and display active sessions in the code example that follows.

To find and display active sessions

  1. Create an instance of the FPC COM object, which provides access to the other Forefront TMG administration COM objects.
  2. Declare an FPCArray object, an FPCSessionsMonitor collection, an FPCFilterExpressions collection, and an FPCSessionsMonitorEntry object.
  3. Get references to the existing FPCArray object and the FPCSessionsMonitor collection.
  4. Create an FPCFilterExpressions collection.
  5. Call the ExecuteQuery method to start a query to find the active sessions using the empty FPCFilterExpressions collection.
  6. In a loop, display the ClientIP property of each FPCSessionsMonitorEntry object representing a session found in the FPCSessionsMonitor collection.
  7. When all the sessions that existed when the query started have been displayed, call the EndQuery method to stop the query.

The following code can be saved to a .vbs file and run on a computer running Forefront TMG with the Microsoft Firewall service installed. We recommend running this script from a command prompt using the CScript command.

Sub DisplayActiveSessions()
	' Define an enumeration value.
	Const fpcSessionFound = 0
	Const bufferSize = 10000
	' Create the root object.
	Dim root  ' The FPCLib.FPC root object
	Set root = CreateObject("FPC.Root")
	' Declare the other objects needed.
	Dim isaArray		' An FPCArray object
	Dim sessionmonitor  ' An FPCSessionsMonitor collection
	Dim filter		' An FPCFilterExpressions collection
	Dim session		 ' An FPCSessionsMonitorEntry object
	' Get references to the array object and the sessions monitor collection.
	Set isaArray = root.GetContainingArray()
	Set sessionmonitor = isaArray.SessionsMonitors.SessionsMonitorFirewall

	' Create a filter expressions collection.
	Set filter = CreateObject("FPC.FPCFilterExpressions")
	' Start a query to find the active sessions.
	sessionmonitor.ExecuteQuery filter, bufferSize
	WScript.Echo "Finding the active sessions ... Please wait a few seconds."
	Dim Index
	Index = 1

	On Error Resume Next
		Set session = sessionmonitor.Item(Index)
		' An E_PENDING (0x80070002) error is raised when 
		' the index points beyond the end of the current list of sessions.
		If Err.Number <> 0 Then 
			WScript.Echo "All existing sessions have been retrieved."
			Exit Do
		End If
		' An FPCSessionMonitorEntry object can be 
		' related to various session events. Here we 
		' are interested only in entries whose Event
		' property equals fpcSessionFound, which 
		' indicates a session that existed when the 
		' query started. Display the client IP address
		' of each session found.	
		If session.Event = fpcSessionFound Then  
			WScript.Echo "Session with: " & session.ClientIP
		End If 
		Index = Index + 1
	Loop Until Err.Number <> 0
	' Stop the query.
	WScript.Echo "The query has been stopped."
	WScript.Echo "Done!"
End Sub 

Send comments about this topic to Microsoft

Build date: 11/30/2009

© 2008 Microsoft Corporation. All rights reserved.