Guy’s Scripting Ezine 75 –  Display a Computers Shares

Contents for Ezine 75 List Shares

 ♣

This Week’s Secret

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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

This Week’s Mission – To List Network Shares

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example 1 – To List Network Shares

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

  1. Copy and paste the script below into notepad.
  2. Change the name of GuyServer on Line 11.  Set the strServer to the name of your computer.
  3. Save the file with a .vbs extension e.g. ListShare .vbs.
  4. Double click the script, then read the names of the shares in the message box.
 

‘ ListShare.vbs Windows Logon Script
‘ VBScript List Shares on a Server
‘ Author Guy Thomas https://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 Shares

Note 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. 

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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 – To List Network Shares with InputBox

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

  1. Copy and paste the script below into notepad.
  2. Save the file with a .vbs extension e.g. ListShare2.vbs.
  3. 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 https://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 2

Note 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-Takes

If 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

Summary for Listing a Computer’s Shares

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.

See more about logon scripts

MapNetworkDrive  • Ezines  • Logon Map Network Drive  • Logon Scripts  • LEM 

Ezine 32 Remove  • Ezine 60 Rename  • PowerShell Logon Scripts  • Ezine 74 Map

Ezine 75 Shares  • Ezine 87 Map drive  • Ezine 105 Map Arguments  

Ezine 115 Map Groups  • Ezine 132 Assign Logon  • Tool Kit  • Free CSV Importer