DOWNLOAD THE CODE:
Download the Code 38500.zip

Applying What You've Learned
Let's take what you've learned so far and use ADSI to assign resources to people based on their group memberships. To do this, you need to collect the username and domain of the user currently logged on, plug that information into a GetObject function to bind to that user object, then retrieve a list of groups to which that user belongs. After you have that list, you can use VBScript's For Each...Next statement to perform an action for each group in the list. In this case, the action is comparing the group's name with a list of possible group names. If a match occurs, you map the appropriate network resource. You use the same WshNetwork object methods and properties that I showed you how to use in "Connecting Users to Network Resources." Listing 3 shows what the script might look like.

If you customize and run the script SetResource.vbs in Listing 3, it will assign network resources based on the group to which the user belongs. However, if your users belong to more than one group, you'll run into problems using this script as is. SetResource.vbs maps a drive letter to a network resource based on the user's group; if the script encounters a second group for the user, the script can't map the drive again. Instead, the script will end with an error. To avoid this problem, you have two options: You can prevent SetResource.vbs from trying to map a second network resource to the same user, or you can unmap the existing network drive before mapping the new one.

Preventing a second-mapping attempt. To prevent the script from trying to map a second network resource to the same user, you can use WScript object's Quit method to terminate the script after a match occurs. Except for the Case Else clause (which already includes the Quit method), you need to add the Quit method at the end of each case, as the following code shows:

Case "Domain Users"
  oNet.MapNetworkDrive "X:", _
  "\\server\users"
 WScript.Quit

However, if your script performs other tasks after mapping a network resource, you won't want the script to terminate after it maps the resource. In such cases, you can use the Exit statement to exit the For Each...Next loop. After a mapping occurs, the script stops running through the group names and proceeds to the code that follows the Next clause. To implement this approach, you add at the end of each case an Exit statement that includes the For keyword, as the following code shows:

Case "Domain Users"
  oNet.MapNetworkDrive "X:", _
  "\\server\users"
 Exit For

Because the Case Else clause already includes the Quit method, you wouldn't add an Exit statement to the Case Else clause.

No matter whether you use the Quit method or the Exit statement, you need to be careful about the order in which you place the cases in the Select Case statement. Let's assume that you want those users who are in multiple groups to be mapped to the most powerful network resource available to them. For both the Quit method and the Exit statement, you must put the most powerful group first. For example, if you're matching against two groups—Domain Users and Domain Administrators—you would put the Case "Domain Administrators" code first, followed by the Case "Domain Users" code. (The Case Else clause always goes last.)

Unmapping an existing network drive. If your users belong to more than one group, you can prevent SetResource.vbs from erroring out because of an existing network drive. To do so, you unmap the existing drive, then map to the new drive. This process uses the WshNetwork object's RemoveNetworkDrive method, as the following code shows:

Case "Domain Administrators"
  oNet.RemoveNetworkDrive "X:"
  oNet.MapNetworkDrive "X:", _
   "\\server\users"

Like the Quit method and the Exit statement, the RemoveNetworkDrive method requires that you place the cases in the Select Case statement in a specific order. However, when you use the RemoveNetworkDrive method, you must place the most powerful group last and the least powerful group first. In addition, you don't include the RemoveNetworkDrive method in the code for the first case.

What's in Common?
If you want to assign network resources to more than one user or computer at a time, you can look for elements common to those users or computers. Whether
the common elements are groups to which users belong, computer names based on location or function, or something else, you can take advantage of the Select Case statement to assign network resources.


Related Reading
You can obtain the following articles from Windows & .NET Magazine's Web site at http://www.winnetmag.com.

BOB WELLS
Scripting Solutions, "Easy Active Directory Scripting
for Systems Administrators, Part 2,"
November 2000, InstantDoc ID 15734

Scripting Solutions, "Easy Active Directory Scripting
for Systems Administrators, Part 1,"
September 2000, InstantDoc ID 9168

You can obtain the following articles from Windows Scripting Solution's Web site at http://www.winscriptingsolutions.com.

ALISTAIR G. LOWE-NORRIS
"An ADSI Primer, Part 12: Extending the Schema,"
December 1999, InstantDoc ID 7666

"An ADSI Primer, Part 11: More on Scripting
Permissions and Auditing," November 1999, InstantDoc ID 7456

"An ADSI Primer, Part 10: Permissions and
Auditing Basics," October 1999, InstantDoc ID 6188

"An ADSI Primer, Part 9: Manipulating Print Queues
and Print Jobs," September 1999, InstantDoc ID 6128

"An ADSI Primer, Part 8: More About Manipulating
Persistent and Dynamic Objects," August 1999, InstantDoc ID 5878

"An ADSI Primer, Part 7: Manipulating Persistent and
Dynamic Objects," July 1999, InstantDoc ID 5629

"An ADSI Primer, Part 6: Using ADSI to Create and
Manipulate User Accounts," June 1999, InstantDoc ID 5456

"An ADSI Primer, Part 5: Using ADO to Script Fast
ADSI Searches," May 1999, InstantDoc ID 5281

"An ADSI Primer, Part 4: Manipulating the Property
Cache," April 1999, InstantDoc ID 5126

"An ADSI Primer, Part 3: IADs and the Property Cache,"
March 1999, InstantDoc ID 4991

"An ADSI Primer, Part 2: Manipulating Active Directory Objects,"
February 1999, InstantDoc ID 4818

"An ADSI Primer, Part 1," January 1999, InstantDoc ID 4732


End of Article

Prev. page     1 2 [3]     next page -->



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

No scripting on setting default printer appears.

MrTwistoff

Article Rating 1 out of 5

 
 

ADS BY GOOGLE