As shown in Figure 7.4, with Server Manager you can view all sessions associated with a particular resource by clicking the Users button in the Properties For dialog box. Using the IADsFileServiceOperations and IADsSession interfaces, you can also view this data programmatically.
The ability to monitor inbound connections can be useful in many ways for an application developer. For developing HTML versions of standard Windows NT administrative tools, the ability to re-create this dialog box using ADSI adds to the richness of your administrative application. This same function could be applied to determine the number of inbound connections to a particular machine for product licensing purposes.
Note
Not surprisingly, all properties of the IADsSession interface are read-only; they are dynamically manipulated by the system, not the user .
To view all active connections to a specific machine, first bind to the LanmanServer container and then enumerate the session objects contained within.
The following Visual Basic code enumerates all sessions to a given machine as well as the user responsible for the connection, the computer from which they are associated, and the connect and idle times (in seconds):
Dim FileService As IADsFileServiceOperations Dim ComputerName As String Dim ComputerDomain As String Dim Session As IADsSession ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " Set FileService = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") For Each Session In FileService.Sessions Debug.Print "Session Name:"&Session.Name&" established by User: "& Session.User&_ " from Computer: "&Session.Computer&" Connect Time: "&Session.ConnectTime& _ " Idle Time: "&Session.IdleTime Next
In the preceding code example, all the exposed properties of the IADsSession interface in the Windows NT service provider were displayed. Let's look at the function of each one in a bit more detail:
User . This property describes the user who instantiated the connection.
Computer . This property specifies the computer name of the connected user's computer.
ConnectTime . This property specifies the number of seconds that have elapsed since the connection was opened.
IdleTime . This property describes the time elapsed since the user last initiated an action on the connection.
Name . This property is a dual interface with the IADs interface and is required for binding to an individual session.
Unlike the majority of interfaces in ADSI, the IADsSession interface is created dynamically. To overcome this minor obstacle , you must use the IADsCollection interface to add and remove objects from the collection. This enables you to manage user sessions.
Additionally, the ability to query a single user session can improve performance significantly over enumeration methods on network file servers where there are likely to be hundreds (if not thousands) of sessions open .
Use the following Visual Basic code to bind to a specific user session:
Dim FileService As IADsFileServiceOperations Dim ComputerName As String Dim ComputerDomain As String Dim Session As IADsSession Dim Collection As IADsCollection Dim UserSessionName As String Dim UserSession As IADsSession ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " UserSessionName = " Target_Session " Set FileService = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") Set Collection = FileService.Sessions Set UserSession = Collection.GetObject(UserSessionName)
As an administrator, there may be occasions when you need to disconnect a single user from a shared resource. Using Server Manager, this is easily performed by simply clicking the Disconnect button while the desired session is highlighted in the User Sessions dialog box. You can also perform this task programmatically by removing the desired session from the collection. To perform this task using Visual Basic, use the following code:
Dim FileService As IADsFileServiceOperations Dim ComputerName As String Dim ComputerDomain As String Dim Session As IADsSession Dim Collection As IADsCollection Dim UserSessionName As String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " UserSessionName = " Target_Session " Set FileService = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") Set Collection = FileService.Sessions Collection.Remove (UserSessionName)
Tip
Windows NT/2000 will transparently reconnect the user session when the client requests the resource again. This code is best executed immediately before the action requiring you to disconnect the user session, such as backing up an open file or updating a .DLL that is currently in use by a remote user.
Server Manager also allows you to remove all user sessions. To perform this task programmatically, combine the task of removing an item from the collection with an enumeration function to remove all items within the collection.
Use the following Visual Basic code to disconnect all user sessions from a given server:
Dim FileService As IADsFileServiceOperations Dim ComputerName As String Dim ComputerDomain As String Dim Session As IADsSession Dim Collection As IADsCollection ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " Set FileService = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") Set Collection = FileService.Sessions For Each Session In Collection Collection.Remove (Session.Name) Next
Top |