Solved

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

Posted on 2004-10-01
2
869 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

688 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