How Can I List Open Sessions and Open Files on a Computer?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there a way to export the information found in the Shared Folders portion of the Computer Management snap-in (in particular, the list of sessions and the list of open files)?

— PP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PP. Funny you should ask; this very issue came up the other day, and while we assumed that there was a way to get at this information we weren’t totally sure about that. As it turns out, though, there are actually a couple different ways to get at this data; for now we’ll show you one approach that can retrieve this information off of Windows 2000 computers as well as Windows XP and Windows 2003 computers.


So how do we get at the sessions as shown in the Shared Folders snap-in? One way is to use the IADsSession interface. That sounds very technical, but it’s actually pretty easy. For example, here’s a script that binds to the file service on the computer atl-ws-01 and then echoes back information about each session. Note that in our binding string we connect to LanmanServer; that’s because – in the registry – LanmanServer is the name used by the file service. Here’s the script:

Set objConnection = GetObject(“WinNT://atl-ws-01/LanmanServer”)
Set colSessions = objConnection.Sessions

For Each objSession in colSessions
Wscript.Echo “Computer: ” & objSession.Computer
Wscript.Echo “Connected Time: ” & objSession.ConnectTime
Wscript.Echo “Idle Time: ” & objSession.IdleTime
Wscript.Echo “Name: ” & objSession.Name
Wscript.Echo “User: ” & objSession.User
Wscript.Echo
Next


We begin by binding to the file service; after making the connection the script then retrieves a collection of open sessions. That’s what this line of code does:

Set colSessions = objConnection.Sessions

The rest of the script is simply a matter of walking through that collection and echoing back the property values for each open session. (To tell you the truth, we thought it was going to be harder than that, too; like we said, this is almost too easy.)


Incidentally, if you want to verify that the script is returning the correct data, type net sessions at the command prompt; the data brought back by your script and the data brought back by Net.exe should be the same. Likewise, you can verify that your script is returning the correct information about open files by typing net files from the command prompt.


Oh, yeah: what about open files? Turns out we can use the IADsResource interface to get the set of open files:

Set objConnection = GetObject(“WinNT://atl-ws-01/LanmanServer”)
Set colResources = objConnection.Resources

For Each objResource in colResources
Wscript.Echo “Path: ” & objResource.Path
Wscript.Echo “User: ” & objResource.User
Wscript.Echo
Next


As you can see, this is very similar to the script that returns the open sessions; the only differences are: 1) We retrieve a collection of resources rather than sessions; and, 2) We echo a different set of property values. But that makes sense: after all, open files are going to have different properties and property values than open sessions. There you go: open files and open sessions.


Of course, one thing we’ve learned in writing this column is that the moment we answer one question people respond with 50 or 60 additional questions related to that topic. Because of that we know we’re bound to get questions like, “How can I disconnect all these sessions?” or “How can I close all these files?” Without going in any detail, here’s a sample script that closes all the open sessions on a computer:

Set objConnection = GetObject(“WinNT://atl-ws-01/LanmanServer”)
Set colSessions = objConnection.Sessions

For Each objSession in colSessions
colSessions.Remove(objSession.Name)
Next


And here’s one that closes all the open files:

Set objConnection = GetObject(“WinNT://atl-ws-01/LanmanServer”)
Set colResources = objConnection.Resources

For Each objResource in colResources
colResources.Remove(objResource.Name)
Next


We should point out that when we tested these close session and close file scripts they seemed to work just fine on Windows 2000 Server and Windows Server 2003. Results on Windows XP, however, were mixed at best. So, no guarantees; give them a try and see if they work in your environment.