monitor file share using vbscript

Hello All,

Can anyone assist with a vbscript to check the open connections or open files on a server? I
am able to retrieve the information on ALL open files but I am only interested in files/folders
that are open from one share. This is my script that brings back everything:
'Connect to filer
Set objConnection = GetObject("WinNT://Mydomain/MyFilerName/LanmanServer")
Set colResources = objConnection.Resources
'Setup logfile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("NetApp3.csv", _ 
    ForWriting, True)
'Set column headings
	objLogFile.Write "Path" & "," & "User" & "," & vbCrlf
'Get objects and write to file.
For Each objResource in colResources
    objLogFile.Write objResource.Path & ","
    objLogFile.Write objResource.User & ","
	objLogFile.WriteLine
Next
'Close logfile
objLogFile.Close

Open in new window


This is a sample of output showing the path and username that is holding the file/folder open.

Path,User,
C:\vol\group01\Foldername\etc,User111
C:\vol\group02\Homes\etc,User111
C:\vol\group04\profiles,User321
..\Truncated!

Open in new window


The shared folders are named 'groupxx'. For one of these shares I only want to monitor
access through the top level directory. However, for another share I want to monitor what is
being accessed all the way through the directory structure.

I plan to run a scheduled task every 30 minutes or so.

Can anyone assist?
Many thanks
Tom
LVL 3
Tommy_CooperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
In order to filter on a specific part of the file path, you need to do the following inside the for each loop.

For Each objResource in colResources
	If InStr(1, objResource.Path, strFilter, VBTextCompare) Then
		objLogFile.Write objResource.Path & ","
		objLogFile.Write objResource.User & ","
		objLogFile.WriteLine
	End If
Next

Open in new window


The part of the path (share name) should be set in the "strFilter" variable.  For example:

strFilter = "Group67"

Open in new window


The InStr() function will start at position 1 of the objResource.Path and look for the strFilter that is defined.  The VBTextCompare does a case-insensitive compare.  Meaning "group67" is the same as "GROUP67"

InStr reference:  http://www.devguru.com/technologies/vbscript/13934

There is a correction to the objConnection call.  In the case, it should be:

Set objConnection = GetObject("WinNT://<YourServerName>/LanmanServer")

Open in new window


I like to explicitly declare everything in my VBScripts, so below is the full text of the script:

'###### Script setup - START ######
Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim objConnection
Dim colResources
Dim objFSO
Dim objLogFile
Dim objResource
Dim strFilter

'Connect to filer
'Set objConnection = GetObject("WinNT://<YourServerName>/LanmanServer")
Set colResources = objConnection.Resources
strFilter = "<YourFilterHere>"


'Setup logfile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("NetApp3.csv", ForWriting, True)

'Set column headings
objLogFile.Write "Path" & "," & "User" & "," & vbCrlf

'Get objects and write to file.
For Each objResource in colResources
	If InStr(1, objResource.Path, strFilter, VBTextCompare) Then
		objLogFile.Write objResource.Path & ","
		objLogFile.Write objResource.User & ","
		objLogFile.WriteLine
		
		' *** Call below closes the open file on the share, uncomment to function ***
		'colResources.Remove(objResource.Name)
	End If
Next

'Close logfile
objLogFile.Close

'Destroy Objects
Set objLogFile = Nothing
Set objFSO = Nothing
Set objResource = Nothing
Set colResources = Nothing
Set objConnection = Nothing

Open in new window


I have commented out the closing of the open file, so you can run it against a server as a test and check the output file.  If you are satisfied with the functionality, I would un-comment the line containing "colResources.Remove" and test it against a share and a file that is not in production.

Update the "strFilter" and you objConnection path.

Link to a reference article: http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/16/how-can-i-list-open-sessions-and-open-files-on-a-computer.aspx

Dan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tommy_CooperAuthor Commented:
Dan - That is awesome. Thank you ever so much. I will give it a test tonight or in the morning (GMT).
Tommy_CooperAuthor Commented:
Dan,

You're a star! Many many thanks for your help, assistance and the time you spent on this.

It's kind of working fine. Sadly, I have no visibility of the target NetApp filers. But I'm running this every 30 minutes against a number of Netapps and at random times the filers will not return any info. I suspect this is because they are too busy but can't tell for sure. Hopefully the limited output will be enough to fulfill my bosses needs!

Sorry for the delay in reporting back, got caught up in other issues!

Cheers
Tom
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.