Previous Section Table of Contents Next Section

Listing Remote Shares

Ever wonder what shares are available on a remote file server? I've often needed a complete list. Yes, you can use NET VIEW or another command-line utility, but what if you want to list several servers at once, or have the list of shares output to a text file, or used by another script? Having a script capable of generating this list can be a handy tool.

graphics/arrow.gif Listing Shares

Listing 31.2 shows how to pull a list of shares from any remote computer, using ADSI.

Listing 31.2. Shares.vbs. Listing remote shares.

sServerName = _

 InputBox("Enter name of server to list shares for.")



Set oFS = GetObject("WinNT://" & sServerName & _

 "/LanmanServer,FileService")

For Each sSh In oFS

      WScript.Echo sSh.name

Next

Not very complicated, is it? That's the power of ADSI. You shouldn't have to make any changes to run this script, and it will run on NT, 2000, XP, and 2003 systems.

graphics/arrow.gif Listing Shares-Explained

This script starts out by simply asking for the name of the server that you want to list shares for.


sServerName = InputBox("Enter name of server to list shares for.")

Next, it queries ADSI. The ADSI query connects to the specified server's LanManServer, which is a file service. Physically, the Server service is present on all Windows NT-based computers, including NT, 2000, XP, and 2003.


Set oFS = GetObject("WinNT://" & sServerName & _

 "/LanmanServer,FileService")

The file service has a collection of shares, and this next loop simply iterates each of them and displays each in a message box (or outputs to the command line, if you're running through Cscript.exe).


For Each sSh In oFS

      WScript.Echo sSh.name

Next

You can customize this script easily. For example, to output the server's shares to a text file, just modify the latter half of the script as follows.


Dim oFSO, oTS

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = oFSO.CreateTextFile("c:\shares")



oTS.WriteLine "Shares for server " & sServerName

For Each sSh in oFS

 oTS.WriteLine sSh.Name

Next

oTS.Close

You can modify the script to read a list of servers and output each of their file share lists. Listing 31.3 shows the complete script.

Listing 31.3. ListShares.vbs. Listing shares for servers from a text file.

Dim oFSO, oTSIn, oTSOut

Set oFSO = CreateObject("Scripting.FileSystemObject")



'Create output file

Set oTSOut = oFSO.CreateTextFile("C:\shares.txt")



'Open input file

Set oTSIn = oFSO.OpenTextFile("c:\servers.txt")



'go through servers

Do Until oTSIn.AtEndOfStream



'get server name

Dim sServerName

sServerName = oTSIn.ReadLine



 Dim oFS, sSH

 Set oFS = GetObject("WinNT://" & sServerName & _

  "/LanmanServer,FileService")



 'go through shares

 For Each sSh in oFS

  oTSOut.WriteLine sSh.Name

 Next



 Set oFS = Nothing



Loop



'close files

oTSIn.Close

oTSOut.Close



'finished!

MsgBox "Finished!"

The input file in this example should list one server name per line, with as many servers as you like.

    Previous Section Table of Contents Next Section