|
|
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 groupsDomain Users and Domain Administratorsyou 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.
End of Article
Prev. page
1
2
[3]
next page -->
|
|
|
|