Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Ezine 75 List Shares
This week's secret is keep it simple and pay attention to detail. My idea is straightforward, to build a VBScript which lists a computer's shares.
However, rarely has such a short script given me so many problems. Fortunately, in the finished examples, you cannot see all my failed experiments and blind alleys; however if you are interested, I do have an online page of those out-takes. As ever, you have a choice. Either, you can just copy and paste Example 2, or you can start at
Example 1 and discover how I built the script in stages. What would make my day, is if you scanned my learning points, so that you could apply the lessons to your scripts.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
What this week's script will do is to display a server's shared folders and printers. In the first example, I have hard-coded the name of the server
in the script itself. The second script not only provides an inputbox where you can specify different servers, but it also displays the shares in one long list. The first example is easy scripting, but
the output is ponderous because it displays each share in turn. In fact, Example 1 drove me mad because I had to keep clicking OK on each share's message box. The second script exploits the loop, builds a
list of all shares and finally displays all the names in one long list - much better.
Here is a simple script, which will interrogate the operating system and then echo each share to a message box. There is one pre-requisite. Naturally, you must have a shared folder or printer on the machine that you run
the script. Instructions for displaying a servers shares - Copy and paste the script below into notepad.
-
Change the name of GuyServer on Line 11. Set the strServer to the name of your computer.
- Save the file with a .vbs extension e.g. ListShare .vbs.
- Double click the script, then read the names of the shares in the message box.
For a top Script Editor try a free download at OnScript
' ListShare.vbs Windows Logon Script ' VBScript List Shares on a Server ' Author Guy Thomas http://computerperformance.co.uk/ ' Ezine 75 Version 1.3 -
May 2005 ' ----------------------------------------------------' Option Explicit Dim objFs, objShare Dim strServer
' The section sets the variables strServer = "GuyServer"
'
Connects to the operating system's file system set objFs = GetObject("WinNT://" _ & strServer & "/LanmanServer,FileService")
' Loops through each share For Each objShare In objFs
WScript.Echo objShare.name Next
End of List Share VBScript
Learning Points for Listing SharesNote 0: Do get a good VBScript editor. OnScript will not only teach about scripting, but also provide more free examples of scripts like the
above.
For a top Script Editor try a free download at OnScript
Note 1: The key to this script, is the way it connects to the operating system, by employing this command:
GetObject("WinNT://" & strServer & "/LanmanServer,FileService"). The majority of my scripts use not WinNT, but LDAP://. This is because they use LDAP to query active directory, whereas this script queries the
file system otherwise known as LanmanServer,FileService. Note 2: I will let you into a secret, I just could not resist substituting LDAP:// for WinNT://. As expected it failed.
Now here is an important Learning Point, when I changed the script back to WINNT:// it did not work. Eventually I realized that just the WinNT:// part is case sensitive, WINNT grrrrrrrrrrr. Note 3: The For Each.... Next is a
classic loop for interrogating the operating system. Pay particular attention to the tiny word 'Each'. For Each objShare In objFs. (Not: For ObjShare In ObjFS.) Perhaps you are
beginning to see what I mean by paying attention to detail. Note 4: Whilst the For Each.. Next is an elegant scripting technique, the output from this example is ugly. That is why in Example
2, I am going to add the strList variable to build up a list before Echoing the output: strList = strList & vbCr & LCase(objShare.name).
Example 2 is the complete script. It features a neat inputbox, and a clear display of all the shares in one box. Instructions for displaying a list of a
Computers shared drives - Copy and paste the script below into notepad.
- Save the file with a .vbs extension e.g. ListShare2.vbs.
- Double click, enter the name of the server in the dialog box, then read the names of the shares in the message box.
' ListShare2.vbs Windows Logon Script ' VBScript List Shares on a Server ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.7 - May 2005
' ----------------------------------------------------' Option Explicit
Dim objFs, objShare Dim strServer, strList
' The section sets the variables strList = "
--------------------------- " strServer = "GuyServer"
' Creates the Input Message Box strServer = InputBox("Server to list shares: "_ ," Share List", strServer)
' Connects to the
operating system's file system set objFs = GetObject("WinNT://" _ & strServer & "/LanmanServer,FileService")
' Loops through each share For Each objShare In objFs strList = strList & vbCr &
LCase(objShare.name) _ & " " & vbTab & UCase(objShare.Description) Next
WScript.Echo strList
Learning Points for Example 2Note 1: This script builds on Example 1. To make the input easier and more flexible I added InputBox. Picking up on my attention to
detail theme, I wanted the InputBox to display strServer as the default. At first, I forgot that the default server is the third parameter; the second parameter is merely the InputBox's title. So
why this failed,
strServer = InputBox("Server to list shares: ", strServer) puzzled me until I saw strServer in the Title of the Inputbox. The correct syntax is strServer = InputBox("Server to list shares: "," Share
List", strServer) Note 2: As promised, I added a command to build up share list, rather than displaying each one individually. strList = strList & vbCr & LCase(objShare.name).
Note also how the WScript.Echo strList is now outside the loop. Note 3: I wanted to draw your attention to the syntax, object.property. So, I added objShare.description to the objShare.name.
Whilst this is a relatively trivial point, it drove me mad when I inadvertently omitted the .property, for example: strList = strList & vbCr & LCase(objShare) failed with an 800A01C2 error message. It
should be LCase(objShare.name) Note 4: Incidentally, I expect that you guessed LCase means lower case and UCase means upper case, I only wish there was a Proper Case command.
Challenges.It should not be too difficult to output the list to a text file. If you like a challenge, try adding FSO commands to write the list to a file. I attempted to get VBScript to display
the list of shares in alphabetical order, unfortunately
I failed, but I still believe that it is possible. If only there was more time! So, if you do discover how to script the sort order, do let me know. Out-TakesIf you would like a to
test your scripting skills, then I have two scripts with deliberate mistakes for you to correct. See Out-Takes online
Here is a neat script to display a list of all the shared folders and computers on a server. From a VBScript point of view, the script demonstrates looping, inputbox and employing WinNT to
interrogate the operating system.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|