DekGenius.com
[ Team LiB ] Previous Section Next Section

22.2 Creating and Manipulating Shares with ADSI

The following code shows how easily you can create shares with ADSI:

Dim objComputer, objFileShare
   
Set objComputer = GetObject("WinNT://mydomainorworkgroup/mycomputer/LanmanServer")
   
Set objFileShare = objComputer.Create("FileShare", "MyNewShare")
objFileShare.Path = "c:\mydirectory"
objFileShare.Description = "My new Share"
objFileShare.MaxUserCount = 8
objFileShare.SetInfo

After we declare the objComputer and objFileShare variables, we bind to the LanmanServer object on the computer on which we want to create the shares. LanmanServer is the object name of the server service that runs on all Windows NT and later computers. We bind to this object because NT's predecessor was LAN Manager and is still present to a large extent in the Windows OS.

Next, we use the IADsContainer::Create method to create an object of class FileShare and apply the IADsFileShare property methods to set the path, description, and maximum number of users. On an NT, Windows 2000, or Windows Server 2003 server, you can grant all users access to a share or limit access to as many users as you want. On a workstation, you can grant all users access to a share or limit access to between 1 and 10 users at a time. The latter restriction is due to the 10-connection limit that the OS imposes. The values that the IADsFileShare::MaxUserCount method accepts are -1 (which grants all users access), any numerical value between 1 and 10 on workstations, and, within reason, any numerical value on the server family of OSs.

Finally, we end the script with IADs::SetInfo, which writes the information from the property cache to the directory.

Enumerating existing shares is just as easy as creating them. The next piece of code shows how to enumerate normal shares.[1]

[1] Hidden shares aren't shown due to their very nature.

Dim objService, objFileShare, strOutput
   
strOutput = ""
   
Set objService = GetObject("WinNT://workgroup/vicky/LanmanServer")
   
For Each objFileShare In objService
  strOutput = strOutput & "Name of share : " & objFileShare.Name & vbCrLf
  strOutput = strOutput & "Path to share : " & objFileShare.Path & vbCrLf
  strOutput = strOutput & "Description : " & objFileShare.Description & vbCrLf
   
  If objFileShare.MaxUserCount = -1 Then
    strOutput = strOutput & "Max users : No limit" & vbCrLf
  Else
    strOutput = strOutput & "Max users : " & objFileShare.MaxUserCount & vbCrLf
  End If
   
  strOutput = strOutput & "Host Computer : " _
    & objFileShare.HostComputer & vbCrLf & vbCrLf
Next
   
WScript.Echo strOutput

This code is similar to that in the previous script for creating a share. This is a sample of the output:

Name of share : NETLOGON
Path to share : C:\WINNT35\system32\Repl\Import\Scripts
Description   : Logon server share 
Max users     : No limit
Host Computer : WinNT://WORKGROUP/VICKY
   
Name of share : Add-ins
Path to share : C:\exchsrvr\ADD-INS
Description   : "Access to EDK objects"
Max users     : No limit
Host Computer : WinNT://WORKGROUP/VICKY
   
Name of share : Logs
Path to share : C:\exchsrvr\tracking.log
Description   : "Exchange message tracking logs"
Max users     : No limit
Host Computer : WinNT://WORKGROUP/VICKY
   
Name of share : Resources
Path to share : C:\exchsrvr\RES
Description   : "Event logging files"
Max users     : No limit
Host Computer : WinNT://WORKGROUP/VICKY
   
Name of share : Drivers
Path to share : C:\WINNT\system32\spool\drivers
Description   : Printer Drivers
Max users     : No limit
Host Computer : WinNT://WORKGROUP/VICKY
   
Name of share : Clients
Path to share : C:\clients
Description   : Network Client Distribution Share
Max users     : No limit
Host Computer : WinNT://WORKGROUP/VICKY
    [ Team LiB ] Previous Section Next Section