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

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

  • Comments 13
  • Likes

 

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.


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Please provide Links to methods and properties please

  • Thank you for the nice description.

    I am getting error message "Active Directory: Not implemented" when i try to run the script for deleting open files.

    Script for deleting sessions is working fine.

    Please suggest.

  • sometimes it's wrong to free up resources without closing the applications

    what can do if i want to know about the sessions holding file-locks (in order to stop the application at the client side)

    i search for a workflow like this   get the open resource  ==> get the corresponding session ==> get the computername for this session and then stop the application

  • I am getting this error message too "Active Directory: Not implemented" when i try to run the script for deleting open files.

  • How can this be done using powershell?

  • I use the good old "NET FILE " CLI command to replace the "Remove" method.  Tested ok on a 2003 R2 server.

    Set objShell = WScript.CreateObject("WScript.Shell")  

    Set objConnection = GetObject("WinNT://atl-ws-01/LanmanServer")

    Set colResources = objConnection.Resources

    For Each objResource in colResources

     strResName = ""

     intLock = 0

     On Error Resume Next

     strResName = objResource.Name

     intLock = objResource.LockCount

     On Error Goto 0

     if intLock <> 0 Then

       strCommand = "net file " & strResName & " /close"

       objResult = objShell.Run(strCommand, 1, True)

     End If

    Next

  • @stanley the Net* commands are always a good solution -- they are easy to use, powerful, and always available. Great suggestion.

  • Hey, Scripting Guy!, Happy New Year :)

    For Open Files script I would like to know whether there is a way to get Open Files for specific folder rather than all the folder which are shared?

    Please help as I am new to VB but cant figure out how to do it..if you can redirect me to anyother link would be appreciated. Thank you!!

    Regards,

    EL

  • @Elias - this cannot be done with scripting.  You need to use the low-level API or OH.EXE from the resource kit.

  • Hey JV, Thank you for your prompt reply :)

    Regards,

    EL

  • Thanks for all the great examples.  Is there any way to do this for local fiiles only? (i.e. I want to get a list of all files that are open on the Windows desktop where the script is running).  Thanks.

  • Perfect, I've been looking for a way to list not only the username associated with the "file share" connection but the computer (or IP), even from remote file servers. I've tried this for file shares on Windows 2003,2008,2012,XP,7 and even a high end EMC NAS filer. Works like a charm.

    Thanks

  • Thats helped me out a great deal. Could you tell me the objresource name for the value under the 'Open mode' column? I need to report on the 'no access' items.

    Thanks