Enumerating Shared Folders

Microsoft® Windows® 2000 Scripting Guide

For the most part, you should not share out every single folder on your computer. After all, doing so exposes all your documents and all your data to anyone browsing the network. Likewise, it potentially allows anyone to make changes to critical or confidential files and folders.

To guard against the possible misuse of shared folders, it is a good idea to periodically review the shared folders on a computer and then decide whether those folders should be shared at all. You can use the Win32_Share class to return information about the shared folders on a computer. The shared folder properties available through this class are shown in Table 11.11.

Table 11.11 Win32_Share Properties

Property

Description

AllowMaximum

Indicates whether or not the number of users allowed to simultaneously access this folder has been limited. If True, the value in the MaximumAllowed property is ignored.

Caption

Description of the object.

MaximumAllowed

Limit on the maximum number of users allowed to use this resource concurrently. The value is valid only if the AllowMaximum property is set to False.

Name

Network name given to the shared folder.

Path

Local path of the shared folder.

Type

Type of resource being shared. Types include disk drives, print queues, interprocess communications (IPC), and general devices. Values are:

0 - Disk drive

1 - Print queue

2 - Device

3 - IPC

2147483648 - Disk drive (Administrative share)

2147483649 - Print queue (Administrative share)

2147483650 - Device (Administrative share)

2147483651 - IPC (Administrative share)

The relationship between the Win32_Share class and Windows Explorer is shown in Figure 11.9.

Figure 11.9 Win32_Share and Windows Explorer

sas_fil_09c

Scripting Steps

Listing 11.32 contains a script that enumerates the properties of all the shared folders on a computer. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Share class.

    This returns a collection of all the shared folders on the computer.

  4. For each shared folder instance in the collection, echo the values for properties such as the shared folder name, the shared folder path, the maximum number of simultaneous connections allowed, and the shared folder type.

Listing 11.32 Enumerating Shared Folders

  
1
2
3
4
5
6
7
8
9
10
11
12


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery("SELECT * FROM Win32_Share")
For Each objShare in colShares
 Wscript.Echo "Allow Maximum: " & vbTab & objShare.AllowMaximum 
 Wscript.Echo "Caption: " & vbTab & objShare.Caption 
 Wscript.Echo "Maximum Allowed: " & vbTab & objShare.MaximumAllowed
 Wscript.Echo "Name: " & vbTab & objShare.Name 
 Wscript.Echo "Path: " & vbTab & objShare.Path 
 Wscript.Echo "Type: " & vbTab & objShare.Type 
Next

Show: