Putting It All Together
With all of these loops and conditional execution constructs under your belt, you're probably ready to see them in action!
Who Has a File?
Listing 10.3 is a sample script that shows you which user or users has a particular file open on a file server.
Listing 10.3. WhoHasFile.vbs. Shows who has a particular file open.
' first, get the server name we want to work with
varServer = InputBox ("Server name to check")
' get the local path of the file to check
varFile= InputBox ("Full path and filename of the file" & _
" on the server (use the local path as if you were" & _
" at the server console)")
' bind to the server's file service
set objFS = GetObject("WinNT://" & varServer & _ "/lanmanserver,fileservice")
' scan through the open resources until we
' locate the file we want
varFoundNone = True
' use a FOR...EACH loop to walk through the
' open resources
For Each objRes in objFS.Resources
' does this resource match the one we're looking for?
If objRes.Path = varFile then
' we found the file - show who's got it
varFoundNone = False
WScript.Echo objRes.Path & " is opened by " & objRes.User
End If
Next
' if we didn't find the file open, display a msg
if varFoundNone = True then
WScript.Echo "Didn't find that file opened by anyone."
end if
Because this script uses an input box to get the server name, you can run it without modification in your environment. Of course, you need to be a Domain Admin or a member of the server's Server Operators group for the script to run; those groups have the permissions necessary to retrieve the information the script requires.
Who Has a File-Explained
The first lines of code simply get the file server's name, and the complete path and filename of the file that you want to check. This file path must start with a drive letter, and cannot be a UNC.
' first, get the server name we want to work with
varServer = InputBox ("Server name to check")
' get the local path of the file to check
varFile= InputBox ("Full path and filename of the file" & _
" on the server (use the local path as if you were" & _
" at the server console)")
The next line of code uses Active Directory Services Interface (ADSI) to connect to the server's file server service. Note that ADSI will work fine even against NT file servers, because it's using the WinNT provider.
' bind to the server's file service
set objFS = GetObject("WinNT://" & varServer & _ "/lanmanserver,fileservice")
If you want to jump ahead and read more about ADSI, head for Chapters 14, 15, and 16.
Next, the script sets a variable to False, meaning it hasn't yet found the file that you're interested in.
' scan through the open resources until we
' locate the file we want
varFoundNone = True
The script uses a For…Next loop to look at each resource opened by the file server service. This is kind of an important concept: When users connect to a file server, the users themselves don't open files. Instead, the file server service (called the Server service in Windows) opens the files on behalf of the user. The file service maintains a collection named Resources that lists each opened file.
' use a FOR...EACH loop to walk through the
' open resources
For Each objRes in objFS.Resources
' does this resource match the one we're looking for?
If objRes.Path = varFile then
' we found the file - show who's got it
varFoundNone = False
WScript.Echo objRes.Path & " is opened by " & objRes.User
End If
Next
Within the For…Next construct, an If…Then construct determines if the current file resource is the one you're interested in.
' use a FOR...EACH loop to walk through the
' open resources
For Each objRes in objFS.Resources
' does this resource match the one we're looking for?
If objRes.Path = varFile then
' we found the file - show who's got it
varFoundNone = False
WScript.Echo objRes.Path & " is opened by " & objRes.User
End If
Next
In other words, does the Path property of the current resource equal the value you provided for the file path and name? If so, the code within the If…Then construct is executed. The variable is set to false, indicating that the script did locate the file you were interested in. The script also displays a message box indicating the user name that has opened the resource. If more than one user has the file open, VBScript continues scanning and displays each user name as it goes through this loop.
' use a FOR...EACH loop to walk through the
' open resources
For Each objRes in objFS.Resources
' does this resource match the one we're looking for?
If objRes.Path = varFile then
' we found the file - show who's got it
varFoundNone = False
WScript.Echo objRes.Path & " is opened by " & objRes.User
End If
Next
Finally, the script winds up with a brief message if the file wasn't found. This is only polite; if you don't include this last bit, the script might not appear to be doing anything if the file wasn't found.
' if we didn't find the file open, display a msg
if varFoundNone = True then
WScript.Echo "Didn't find that file opened by anyone."
end if
NOTE
You may notice the use of WScript.Echo to display messages. This is functionally the same as the MsgBox statement, and you'll learn more about the WScript object in Chapter 11.
As you can see, For…Next and If…Then are powerful tools in this complex and highly useful administrative script.
|