Managing File Resources

   

Managing File Resources

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 .

Binding the LanmanServer Container Using Visual Basic

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, graphics/ccc.gif fileservice") 

Enumerating Shares on a System Using Visual Basic

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.

Figure 7.1. Share Enumeration using Windows NT File Manager.

graphics/07fig01.gif

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.


Binding to a Specific File Share Using Visual Basic

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/ graphics/ccc.gif "&ShareName&",fileshare") 

Querying the File Share Current User Count Using Visual Basic

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/ graphics/ccc.gif "&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.


Querying the File Share Description (Comment Field) Using Visual Basic

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.

Figure 7.2. Share dialog box in Windows NT Explorer.

graphics/07fig02.gif

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/ graphics/ccc.gif "&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.


Setting a New File Share Description Using Visual Basic

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/ graphics/ccc.gif "&ShareName) FileShare.Description = NewFileShareDescription FileShare.SetInfo 

Querying the File Share Host Computer Using Visual Basic

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/ graphics/ccc.gif "&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.


Querying the File Share Maximum Users Property Value Using Visual Basic

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/ graphics/ccc.gif "&ShareName) Debug.Print FileShare.MaxUserCount 

Tip

If the return value is “1, an unlimited number of users can access the share.


Setting a New Value for the File Share Maximum Users Property Value Using Visual Basic

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/ graphics/ccc.gif "&ShareName) FileShare.MaxUserCount = NewFileShareMaximumUsers FileShare.SetInfo 

Querying the File Share Path Property Value Using Visual Basic

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.

Figure 7.3. Viewing the physical path associated with a share using Windows NT Server Manager.

graphics/07fig03.gif

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/ graphics/ccc.gif "&ShareName) Debug.Print FileShare.Path 

Creating a Share Programmatically Using Visual Basic

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 

Removing a Share Using Visual Basic

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


Windows NT. 2000 ADSI Scripting for System Administration
Windows NT/2000 ADSI Scripting for System Administration
ISBN: 1578702194
EAN: 2147483647
Year: 2000
Pages: 194
Authors: Thomas Eck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net