Solved

VBscript - Get Username of client connected to a file on my server

Posted on 2004-10-01
2
864 Views
Last Modified: 2012-05-05
I'm writing this script as an easy way to display who is connected to a specific file on my 2003 server.
I'd like to find out the IP and the USERNAME of the user that is connected to the file.
So far the script works fine for finding the IP of the machines connected, but when I try to get the username it mostly returns IP addresses.

Could someone suggest a better way to do it or perhaps fix my code.

Thanks! :)

*************************************
Option Explicit

Private Const SERVER_NAME = "SERVER1"
Private Const FILE_FILTER = "F:\DIRECTORY\FILE.DAT"

Dim objLanman
Set objLanman = GetObject("WinNT://" & SERVER_NAME & "/LanmanServer")

WScript.Echo "SERVICE FILES ARE OPEN ON THE FOLLOWING COMPUTERS:"

If IsEmpty(objLanman)=False Then
      If GetOpenFiles(FILE_FILTER)=0 Then
            WScript.Echo "  (none)"
      End If
Else
      WScript.Echo "  (none)"
End If

WScript.Quit 0

'
'
'

Private Function GetOpenFiles(FilePath) ''''THIS GETS THE IP ADDRESS OF THE USER CONNECTED TO THE FILE IN QUESTION
      On Error Resume Next
      Dim objResource
      Dim lngReturn, strPath
      lngReturn = 0
      For Each objResource In objLanman.Resources
            Err.Clear
            strPath = objResource.Path
            If Err.number=0 Then                  
                  If Instr(1,strPath,FilePath,1)>0 Then
                        lngReturn = lngReturn + 1
                        'WScript.Echo "  " & objResource.User
                        GetUserSessions objResource.User
                  End If
            End If
      Next
      GetOpenFiles = lngReturn
End Function

Private Function GetUserSessions(User) '''THIS IS WHERE I'M TRYING TO GET THE CLIENTS USERNAME
      On Error Resume Next
      Dim objSession
      Dim lngReturn, strUser
      lngReturn = 0
      For Each objSession In objLanman.Sessions
            Err.Clear
            strUser = objSession.User
            If Err.Number=0 Then
                  If StrComp(User,strUser,1)=0 Then
                        lngReturn = lngReturn + 1
                        WScript.Echo "    " & objSession.Computer
                  End If ' User match
            End If
      Next
      GetUserSessions = lngReturn
End Function
0
Comment
Question by:Matrix1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 5

Accepted Solution

by:
naiea1231 earned 500 total points
ID: 12205963
It might be easier for you to accomplish all of this using  WMI Win32_ServerConnection

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ServerConnection",,48)
For Each objItem in colItems
    Wscript.Echo "ActiveTime: " & objItem.ActiveTime
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "ComputerName: " & objItem.ComputerName
    Wscript.Echo "ConnectionID: " & objItem.ConnectionID
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "InstallDate: " & objItem.InstallDate
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "NumberOfFiles: " & objItem.NumberOfFiles
    Wscript.Echo "NumberOfUsers: " & objItem.NumberOfUsers
    Wscript.Echo "ShareName: " & objItem.ShareName
    Wscript.Echo "Status: " & objItem.Status
    Wscript.Echo "UserName: " & objItem.UserName
Next

Give this a go, it should list all connections to your file server, along with the users connected, and the name of their computers, let me know how it goes. If you need a hand formatting all the output text i'm happy to give you a hand.
0
 
LVL 5

Expert Comment

by:naiea1231
ID: 12210205
Another alternative, using your existing code would be to simply list the logged on user for a particular ip address using something like this


sClient = "192.168.1.103"

who = "winmgmts:{impersonationLevel=impersonate}!//"& sClient &""
Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
for each User in Users
usernamestr = User.Username
Next

MsgBox(usernamestr)


With usernamestr representing the logged on user in the form of "Computer\Username"
This might be an easier option instead of changing your code to make use of Win32_ServerConnection, especially considering your existing script seems to be working perfectly well except for the username part. Hope it's what you need.

James
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question