VBS - List Users Connected to a Share

Hi,

Am looking for a script that I can run that will display the Usernames of users that are currentlly connected to a specific share, I know you can do this just be going to Computer Management but this is for the end users information...

It would be great if you could give some examples...

Many Thanks

Karl

June 13th, 2010 2:45pm

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ServerConnection WHERE ShareName = 'c$'")
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

Or in PowerShell:

Get-WmiObject win32_serverconnection | where {$_.sharename -eq 'c$'} | select username,sharename

Free Windows Admin Tool Kit Click here and download it now
June 13th, 2010 5:25pm

Using the LanmanServer container exposed by the WinNT provider you can get information about shares (Name, Description, CurrentUserCount, MaxUserCount, and Path) and also session information (that includes the user), but I see no way to tell which user is accessing which share. For example:

' Specify NetBIOS names of domain and computer.
strDomain = "MyDomain"
strComputer = "West223"

' Specify a share on the computer.
strShare = "MyShare"

' Bind to the share.
Set objShare = GetObject("WinNT://" & strDomain & "/" & strComputer & "/LanmanServer/" & strShare & ",fileshare")
Wscript.Echo "Share Name: " & objShare.Name
Wscript.Echo "Description: " & objShare.Description
Wscript.Echo "Path: " & objShare.Path
Wscript.Echo "MaxUserCount: " & objShare.MaxUserCount
Wscript.Echo "CurrentUserCount" " & objShare.CurrentUserCount

Set objFileService = GetObject("WinNT://" & strDomain & "/" & strComputer & "/LanmanServer")
For Each objSession in objFileService.Sessions
  Wscript.Echo "Session Name: " & objSession.Name
  Wscript.Echo " Computer: " & objSession.Computer
  Wscript.Echo " User: " & objSession.User
Next

 

Richard Mueller

 

June 13th, 2010 5:56pm

The Win32_ServerConnection class of WMI is a great solution. Substitute NetBIOS name of computer for strComputer to query remote computer in JHofferle's script. Replace '$c' with the name of your share. Requires XP or above. I find ComputerName is the IP address.

Richard Mueller

Free Windows Admin Tool Kit Click here and download it now
June 13th, 2010 10:33pm

Hi,

 

Thanks for the code it works well, I have made some change to it;

 

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ServerConnection WHERE ShareName = 'ShareName13 '")

For Each objItem in colItems
	wscript.echo _
		"ActiveTime: " & objItem.ActiveTime & vbcrlf & _
  		"NumberOfFiles: " & objItem.NumberOfFiles & vbcrlf & _
  		"NumberOfUsers: " & objItem.NumberOfUsers & vbcrlf & _
  		"ShareName: " & objItem.ShareName & vbcrlf & _
		"ComputerName: " & objItem.ComputerName & vbcrlf & _
		"UserName: " & objItem.UserName
Next

This change keeps each field in the same output. If there are two users connected how could I keep the two outputs in the same echo?

Many Thanks

Karl

June 14th, 2010 11:48am

Append the output to a string variable and Echo after the loop. For example:

strOutput = "Share Users:"
For Each objItem in colItems
  strOutput = strOutput _
    & vbCrLf & "ActiveTime: " & objItem.ActiveTime _
    & vbCrLf & "NumberOfFiles: " & objItem.NumberOfFiles _
    & vbCrLf & "NumberOfUsers: " & objItem.NumberOfUsers _
    & vbCrLf & "ShareName: " & objItem.ShareName _
    & vbCrLf & "ComputerName: " & objItem.ComputerName _
    & vbCrLf & "UserName: " & objItem.UserName
Next
Wscript.Echo strOutput

 

Richard Mueller

Free Windows Admin Tool Kit Click here and download it now
June 14th, 2010 12:28pm

i need something similar to this vbscript.

i need a powershell script that will return all users and groups of a specific network share.

this doesn't return anything

Get-WmiObject win32_serverconnection | where {$_.sharename -eq 'c$'} | select username,sharename

August 3rd, 2015 12:37pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics