Solved

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

Posted on 2004-10-01
2
822 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
  • 2
2 Comments
 
LVL 5

Accepted Solution

by:
naiea1231 earned 500 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows 10 SmartScreen vs ClickOnce app 5 132
copyEndy  challenge 15 55
Fibonacci challenge 11 82
word0 challenge 3 56
A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now