Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the Select Case to map drive based on AD Group

Status
Not open for further replies.

Councilk

MIS
May 14, 2003
72
US
Hello everyone,

I'm using a select case statement to check several groups in AD.

If the current user is a member of the group, then the appropriate drive is mapped.

The script appeared to be working but it's still mapping the drive even when the user is no longer a member of a particular group

Any assistance with this issue is greatly appreciated.

Here is the code I'm using for this login script, Heellp!!

============================================================
'Option Explicit
'On error resume next

Dim fServer
Dim home
Dim wshNet
Dim ADSysInfo
Dim CurrentUser
Dim strGroups
Dim GroupMember
Dim strPassP1
Dim StrLogin1
Dim NetStr1

Const TorUsers = "cn=tor-users"
Const TorNTGA = "cn=tor-ntga"
Const TorExec = "cn_Tor-Executive"
Const TorBPPMapK = "cn=Tor-BPP-Drive-Mapping-K"
Const TorBPPMapG = "cn=Tor-BPP-drive-Mapping-G"

fServer = "\\HSNJFSP01"
home = "\Home$"
StrLogin1 = "NTGA$Log.scr"
StrPassP1 = "\\toonfsp01\sys\public\Passport.scr"
NetStr1 = "\\toonfsp01\sys\public"

Set wshNet = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
'wshNet.MapNetworkDrive "Y:", fServer & home & "\" & wshNet.Username

'============ Disco persistent drives ===================
wshNet.RemoveNetworkDrive "T:"
wshNet.RemoveNetworkDrive "G:"
wshNet.RemoveNetworkDrive "K:"
wshNet.RemoveNetworkDrive "X:"
wshNet.RemoveNetworkDrive "R:"
wshNet.RemoveNetworkDrive "I:"
wshNet.RemoveNetworkDrive "S:"
wshNet.RemoveNetworkDrive "H:"
wshNet.RemoveNetworkDrive "N:"

Wscript.sleep 2000
'============ Mapping persistent drives ===================

wshNet.MapNetworkDrive "H:", "\\TOONFSP01\TCTCDATA"
wshNet.MapNetworkDrive "R:", "\\TOONFSP01\TNTCDIST"
wshNet.MapNetworkDrive "X:", "\\TOONFSP01\TNTCAPPS"
wshNet.MapNetworkDrive "J:", "\\$HOMESERVER\DYNAMICS$"
wshNet.MapNetworkDrive "S:", "\\$HOMESERVER\SHARED"
wshNet.MapNetworkDrive "T:", "\\TOONFSP01\MBSGP_Share"
wshNet.MapNetworkDrive "W:", "\\$HOMESERVER\TNTCTNTAPPS"

'========== Current User and group membership =============

strGroups = LCase(Join(CurrentUser.MemberOf))
WScript.Echo(wshNet.UserName & " " & strGroups)
GroupMember = TRUE

Select Case GroupMember
Case K = InStr(strGroups, "TorBPPMapK")
Call TorBPPMapKsub
Case Else Wscript.echo("You should find correct group")
End Select

Select Case GroupMember
Case G = InStr(strGroups, "TorBPPMapG")
Call TorBPPMapGsub
Case Else Wscript.Echo("I don't think this is accurate")
End Select

Select Case GroupMember
Case I = Instr(strGroups, "TorExec")
Call TorExecMapIsub
Case Else Wscript.Echo("This isn't executive level!")
End Select

Select Case GroupMember
Case N = Instr(strGroups, "TorUsers")
Call TorNTGAsub
Case Else Wscript.Echo("Call NTGA script")
End Select

Select Case GroupMember
Case 0 = InStr(strGroups, "TorNTGA")
Call TorNTGAsub
Case Else wscript.echo("Call NTGA script")
End Select

'=================== Drive Mappings =======================

Sub TorBPPMapKsub
WScript.Echo("Made it to BPP!")
wshNet.MapNetworkDrive "K:", "\\CHIFSVP02\Data021"
End Sub

Sub TorBPPMapGsub
WScript.Echo("Okay it's BPP!")
wshNet.MapNetworkDrive "G:", "\\CHIFSVP02\Data02"
End Sub

Sub TorExecMapIsub
WScript.Echo("Made it to Exec!")
wshNet.MapNetworkDrive "I:", "\\TOONFSP01\HOME"
End Sub

Sub TorNTGAsub
WScript.Echo("MAde it to NTGA and USERS")
wshNet.MapNetworkDrive "N:", "\\TOONFSP01\HOME"
End Sub

WScript.Quit







 
Checking the manual for the RemoveNetworkDrive method, it looks like you need to set the optional flag bUpdateProfile to TRUE so that it removes the mapping from the user's profile. You will also probably need to set the bForce boolean to TRUE as well as it is defined before the other. Here is the relevant section:

RemoveNetworkDrive Method said:
object.RemoveNetworkDrive(strName, [bForce], [bUpdateProfile])

Arguments
object
WshNetwork object.

strName
String value indicating the name of the mapped drive you want to remove. The strName parameter can be either a local name or a remote name depending on how the drive is mapped.

bForce
Optional. Boolean value indicating whether to force the removal of the mapped drive. If bForce is supplied and its value is true, this method removes the connections whether the resource is used or not.

bUpdateProfile
Optional. String value indicating whether to remove the mapping from the user's profile. If bUpdateProfile is supplied and its value is true, this mapping is removed from the user profile. bUpdateProfile is false by default.
 
Hello Jet042

I used the switches which helps the script map and unmap a lot better.

This is very helpful with regards to the question of removong the drives however, I raised the group issue first because it's the main area of concern.

My problem appears to be a false positive, where I remove myself from let's say "tor-executive" which has a corresponding drive of "i"

The select case statement evaluates the membership to true and continues to map "i" even when I'm no longer a member of "tor-executive group.

Should I have a profile update statement set earlier in this script to insure the user settings are accurate?

Thanks for your feedback....


 
Ok... First problem is that you are comparing apples and oranges with your select statements...

Case K = InStr(strGroups, "TorBPPMapK")

1) G,I,K, & N are not declared variables
2) InStr returns a numeric value telling the position of the first character being searched for, if the string is not found, then it returns zero.

Next...
[red]Case 0 = InStr(strGroups, "TorNTGA")[/red]
will always return TRUE for non-group-members because InStr is returning zero.

Change your select statements to look for instr(x,y) > 0

Like this...
Code:
Select Case GroupMember
[red]Case InStr(strGroups, "TorBPPMapK") > 0[/red]
  Call TorBPPMapKsub
   Case Else Wscript.echo("You should find correct group")
End Select
    
Select Case GroupMember
[red]Case InStr(strGroups, "TorBPPMapG") > 0[/red]
  Call TorBPPMapGsub
   Case Else Wscript.Echo("I don't think this is accurate")
End Select 
        
Select Case GroupMember
[red]Case Instr(strGroups, "TorExec") > 0[/red]
  Call TorExecMapIsub
   Case Else Wscript.Echo("This isn't executive level!")
End Select
        
Select Case GroupMember
[red]Case Instr(strGroups, "TorUsers") > 0[/red]
  Call TorNTGAsub
   Case Else Wscript.Echo("Call NTGA script")
End Select

Select Case GroupMember
[red]Case InStr(strGroups, "TorNTGA") > 0[/red]
  Call TorNTGAsub
   Case Else wscript.echo("Call NTGA script")
End Select


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
[0] By analogy, this may be a typo.
Const TorExec = "cn_Tor-Executive"
[tt]Const TorExec = "cn[red]=[/red]Tor-Executive"[/tt]

[1] In your switch block, those particles to be searched for are provisioned by a variable, you should not quote them.
[1.1] The use instr() as shown in case sensitive check: just to make sure you know.
[1.2] Testing true/false of the instr() return, inequality (<>) would be slightly more efficient. This is not critical.

>Case InStr(strGroups, "TorBPPMapK") <> 0
[tt]Case InStr(strGroups, TorBPPMapK) <> 0[/tt]

etc etc.

[2] If all those checking are not mutually exclusive, the simpler would be a plain old if-else-end if.
[tt]
If InStr(strGroups, TorBPPMapK) <> 0
Call TorBPPMapKsub
else
Wscript.echo("You should find correct group")
End If
[/tt]
Hence, it would avoid the twist of switch (true) form - a form which won't carry to .net framework, though not a reason to avoid as plague in vbs.
 
Hello JET042

Thanks for clearing up the Case statement where I see what you mean.

I updated the block of code and it still gives a false positive.

I echo the information to the screen to insure the Instr function has information to parse.

The script still brings back a false positive...

It just passes over the drive mapping and I've added myseld to all the groups in this domain
 
False Positive = Logic Problem

Post new script version and results of wscript.echo.

Taking into account my and tsuji's comments script should be good.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
correction
>[self]If InStr(strGroups, TorBPPMapK) <> 0
[tt]If InStr(strGroups, TorBPPMapK) <> 0 [red]then[/red][/tt]
 
Thanks for the help you all, the actual coding was correct.

The False positive was a result of my using the LCase, but the CONSTANTS I used in the reference section of the script was incorrect.

Eg
Const TorUsers = "cn=tor-users"
Const TorNTGA = "cn=tor-ntga"
Const TorExec = "cn_Tor-Executive"
Const TorBPPMapK = "cn=Tor-BPP-Drive-Mapping-K"
Const TorBPPMapG = "cn=Tor-BPP-drive-Mapping-G"

I was able to figure this out because I used the If Then construct provided by JET042 and the groups with lower case were evaluated properly.

The groups with a mixed case, eg Const TorBPPMapK = "cn=Tor-BPP-Drive-Mapping-K"

Were not resolved properly, so this is where the problem was due to my using the LCase in the portion of the script below

eg - strGroups = LCase(Join(CurrentUser.MemberOf))
WScript.Echo(wshNet.UserName & " " & strGroups)
GroupMember = TRUE

I hope this scenario helps someone else if they run into this problem

Thanks again for all your expertise....
 
I didn't actually provide any code. Thanks should be given to PScottC and tsuji instead.
 
Oops! sorry for the misque, it was the code Tsuji provided and ScottP's coments that put me on the right track.

Any information is helpful when trying to use the correct thought patterns to figure this stuff out.

Thanks to all involved in the exchange of information in this thread...

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top