Creating Shared Folders

Microsoft® Windows® 2000 Scripting Guide

Creating shared folders on the local computer is easy. In Windows Explorer, simply right-click a folder, select Sharing and Security, enter the desired share name, and click OK. If you prefer to use the command line, you can use the net share command to accomplish the same task.

Sharing folders on remote computers is a different story; neither Windows Explorer nor net share allows you to share out a folder on another computer. However, you can use WMI and the Win32_Share class to create a shared folder on remote computers as well as on the local computer.

To create a shared folder using WMI, you need to connect to the Win32_Share class and then call the Create method. The Create method requires you to specify the five parameters shown inTable 11.12.

Table 11.12 Shared Folder Parameters

Parameter

Description

Path

Local path to the folder being shared. This folder must already exist or the Create method fails.

Name

Share name used to access the folder over the network. To create a hidden share, append a $ at the end of the name (for example, MyShare$). A hidden share is not visible to users browsing in Network Neighborhood but remains available to anyone who knows that the share exists.

Type

Indicates the type of network share. This is typically one of two values:

0 - Standard network file share. These shared folders can be viewed by anyone on the network. However, you can use share permissions or NTFS permissions to limit access to the contents of the share.

2147483648 - Administrative share. For example, the Administrative share Admin$ resolves to the Windows folder on a computer. Only users with Administrative credentials can connect to these shared folders.

MaximumAllowed

Maximum number of users permitted to connect to the share at one time. If this parameter is left blank, an unlimited number of simultaneous connections to the share are permitted.

Description

Optional description displayed when users view the share in Network Neighborhood.

Values returned from calling the Create method are shown in Table 11.13.

Table 11.13 Network Share Return Values

Value

Description

0

The operation completed successfully.

2

The operation could not be completed because access was denied.

8

The operation could not be completed because of an unknown problem.

9

The operation could not be completed because an invalid name was specified.

10

The operation could not be completed because an invalid level was specified.

21

The operation could not be completed because an invalid parameter was specified.

22

The operation could not be completed because a share by this name already exists.

23

The operation could not be completed because this is a redirected path.

24

The operation could not be completed because the specified folder could not be found.

25

The operation could not be completed because the specified server could not be found.

Other

The operation could not be completed.

Scripting Steps

Listing 11.33 contains a script that creates a shared folder on a computer. To carry out this task, the script must perform the following steps:

  1. Create two constants, FILE_SHARE (with the value 0) and MAXIMUM_CONNECTIONS (with the value 25). FILE_SHARE is used to indicate the type of share being created, and MAXIMUM_CONNECTIONS is used to limit the number of simultaneous connections to the share to 25.

  2. Create a variable to specify the computer name.

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

  4. Use the Get method to query the Win32_Share class.

  5. Use the Create method to create a new share. The method is passed the following parameter values:

    • C:\Finance - Local path of the folder being shared.

    • FinanceShare - Network name to be assigned to the new share.

    • FILE_SHARE - Constant indicating that the new share is a standard network file share.

    • MAXIMUM_CONNECTIONS - Constant setting the maximum number of simultaneous connections to the new share to 25.

    • Public share for members of the Finance group - Description available to users accessing the share through Network Neighborhood.

  6. Echo the results.

Listing 11.33 Creating Shared Folders

  
1
2
3
4
5
6
7
8
9
10


Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 25
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewShare = objWMIService.Get("Win32_Share")
errReturn = objNewShare.Create _
 ("C:\Finance", "FinanceShare", FILE_SHARE, _
 MAXIMUM_CONNECTIONS, "Public share for the Finance group.")
Wscript.Echo errReturn

Show: