Using the IADsFileService, IADsFileServiceOperations, IADsFileShare, IADsSession, and IADsContainer interfaces, you can programmatically create and remove file shares. You can also use these interfaces to view the open sessions and resources managed by a file service, in similar fashion to a computer's properties in Server Manager.
Whether you are creating a Web interface for the management of shares, sessions, and resources, or are creating a share that is part of a larger process automation workflow, ADSI provides an easy method for performing share management tasks .
To enumerate the shares on a machine, you must first bind to the LanmanServer container on a specific computer. This process is exemplified in the following Visual Basic code segment:
Dim FileService As IADsFileService Dim ComputerName As String Dim ComputerDomain As String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " Set FileService = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer, fileservice")
As shown in Figure 7.1, using the Stop Sharing menu entry in Windows File Manager (winfile.exe), you can enumerate all shares on the local machine. Alternatively, you can use the Network Neighborhood icon to show all non-hidden shares for a local or remote computer.
To derive this information programmatically, you can use ADSI to bind the LanmanServer container and then use a For Each loop to enumerate all non-administrative shares on any machine.
To perform this task, use the following Visual Basic code:
Dim FileService As IADsFileService Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " Set FileService = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") For Each FileShare In FileService Debug.Print FileShare.Name Next
Note
After running this code, any hidden shares (those with a "$" appended to the name) created by the user are listed, but system hidden shares (IPC$, ADMIN$, C$, and so on) are not shown. While these shares are indeed hidden from enumeration, you can still bind and manipulate them using the techniques in this chapter.
If you want to bind to a specific service on a machine, first bind the LanmanServer container, and then bind to the name of one of the leaf objects within the container.
To perform this task, use the following Visual Basic code segment as a guide:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName&",fileshare")
By clicking the Shares command button in the computer properties dialog box of Server Manager, you can view the number of users for a particular shared resource on a machine. By querying the CurrentUserCount property of the IADsFileShare interface, you can examine this value programmatically. This is valuable for collecting software usage statistics, deciding whether to take down a file server, or simply satisfying basic curiosity .
Use the following Visual Basic code to determine the number of users currently connected to the target share:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String Dim RetVal as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) RetVal = FileShare.CurrentUserCount Debug.Print RetVal
Note
This property is read-only; the system is responsible for updating the value assigned to this property.
Using the Share As option in Windows File Manager (winfile.exe) or the Sharing option in the Windows NT Explorer (shown in Figure 7.2), you can view and set the comment for a share hosted on a system.
Likewise, you can set the Description property of the IADsFileShare to enter this data from your favorite programming environment, as shown in the following Visual Basic code:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String Dim RetVal as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) RetVal = FileShare.Description Debug.Print RetVal
Tip
If your enterprise uses delimited fields for the description values on servers, you can parse the data using Visual Basic. Use the user description field parsing code segment in Chapter 4, "Programmatic User Account Manipulation," as a guide.
To programmatically set a new description for a file share, use the following Visual Basic code:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String Dim NewFileShareDescription as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " NewFileShareDescription = " New_File_Share_Description " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) FileShare.Description = NewFileShareDescription FileShare.SetInfo
On occasion you may want to verify that the file share you are manipulating belongs to a particular host. To perform this action using ADSI, simply query the value of the HostComputer property as shown in the following Visual Basic code segment:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) Debug.Print FileShare.HostComputer
Note
After running this code segment, you will notice that the ADsPath of the host is returned.
This value should be considered read-only; you cannot create a share on your machine that references a remote host.
As shown in Figure 7.2, Windows NT allows you to configure the maximum number of users allowed to connect to a shared resource.
By default, ADSI creates a share that can be used by an unlimited number of users. In some cases, you may want to limit the number of connections to the share. To query the current value assigned to the MaxUserCount property, use the following Visual Basic code:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) Debug.Print FileShare.MaxUserCount
Tip
If the return value is “1, an unlimited number of users can access the share.
To programmatically assign a new value to the MaxUserCount property of a share, use the following Visual Basic code:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String Dim NewFileShareMaximumUsers as Integer ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " NewFileShareMaximumUsers = Maximum_Users_Value Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) FileShare.MaxUserCount = NewFileShareMaximumUsers FileShare.SetInfo
In the GUI environment, there are many ways to find the path for a share; however, as shown in Figure 7.3, you can display all shares for a particular machine in a single dialog box using Server Manager. To see which path is associated with a particular share name using programmatic methods , you can simply query the Path property of the bound share.
Use the following Visual Basic code to determine the path associated with a specific share:
Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set FileShare = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer/ "&ShareName) Debug.Print FileShare.Path
To create a new share, call the Create method of the IADsContainer interface to create a new entry in the LanmanServer container. Before calling the IADs SetInfo method, you must first assign the IADsFileShare Path property to the value of the physical path you desire the share to reference. This is the only mandatory property for a new share; however, you may want to set a value for the description/comment field at the same time.
Use the following Visual Basic code to create a new share on a host and set the description/comment field to a specific string:
Dim Container As IADsContainer Dim FileShare As IADsFileShare Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String Dim NewFileSharePath as String Dim NewFileShareDescription as String ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " NewFileSharePath = " Path_To_Share " NewFileShareDescription = " File_Share_Comment " Set Container = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") Set FileShare = Container.Create("fileshare", ShareName) FileShare.Path = NewFileSharePath FileShare.Description = NewFileShareDescription FileShare.MaxUserCount = -1 FileShare.SetInfo
If you no longer require a share on a machine, you can bind to the LanmanServer container and remove the object representing the share you wish to remove.
To do this, after binding the LanmanServer container, simply call the Delete method of the IADsContainer interface, as shown in the following Visual Basic code:
Dim Container As IADsContainer Dim ComputerName As String Dim ComputerDomain As String Dim ShareName as String Dim RetVal as Integer ComputerDomain = " Target_Computer_Domain " ComputerName = " Target_Computer_Name " ShareName = " Target_Share_Name " Set Container = GetObject("WinNT://"&ComputerDomain&"/"&ComputerName& "/LanmanServer") Call Container.Delete("fileshare", ShareName)
Top |