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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Login Script to map drives according to Group Membership

Status
Not open for further replies.

kachbo1

IS-IT--Management
Nov 16, 2004
40
GB
I am a bit new to vb scripting.

I am using the following script to map drives for different mebers of different groups. I have about 20 groups and I would like to create one large script to map drives according to group membership. Can somebody help me out with:

1. Is the existing script correct and
2. Show me the correct way.

Kachbo

Set objnetwork = Wscript.CreateObject("WScript.Network")

strUserName = ObjNetwork.UserName

on error resume next

objnetwork.removenetworkdrive "H:"

objnetwork.mapnetworkdrive "H:" , "\\server\users$\" & strUserName , true


If IsMember("Group1") Then

objnetwork.removenetworkdrive "Q:"

objnetwork.mapnetworkdrive "Q:" , "\\server\share$" , True

EndIf

If IsMember("Group2") Then

objnetwork.removenetworkdrive "G:"

objnetwork.mapnetworkdrive "G:" , "\\server\share$" , True

End If

Wscript.Quit
 
You could do you following:

Code:
Set WSHNetwork = CreateObject("WScript.Network")

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

For Each GroupObj In UserObj.Groups
    Select Case GroupObj.Name
        Case "group1"
            WSHNetwork.MapNetworkDrive "G:", "\\server\share1"
            WSHNetwork.MapNetworkDrive "H:", "\\server\share2"
	        WSHNetwork.MapNetworkDrive "S:", "\\server\share3"
            WSHNetwork.MapNetworkDrive "O:", "\\server\share4"

        Case "group2"
            WSHNetwork.MapNetworkDrive "G:", "\\server\share1"
            WSHNetwork.MapNetworkDrive "H:", "\\server\share2"
            WSHNetwork.MapNetworkDrive "O:", "\\server\share5"
            WSHNetwork.MapNetworkDrive "S:", "\\server\share7"
        
        Case "group3"
            WSHNetwork.MapNetworkDrive "R:", "\\server\share9"
    End Select
Next

WScript.Quit
 
I forgot to include removing the mapped drives. I like to take care of it in the beginning and remove all of the mapped drives that I use.

Revised code;
Code:
Set WSHNetwork = CreateObject("WScript.Network")

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

WSHNetwork.RemoveNetworkDrive "E:",true,true
WSHNetwork.RemoveNetworkDrive "G:",true,true
WSHNetwork.RemoveNetworkDrive "H:",true,true
WSHNetwork.RemoveNetworkDrive "J:",true,true
WSHNetwork.RemoveNetworkDrive "L:",true,true
WSHNetwork.RemoveNetworkDrive "O:",true,true
WSHNetwork.RemoveNetworkDrive "R:",true,true
WSHNetwork.RemoveNetworkDrive "S:",true,true
WSHNetwork.RemoveNetworkDrive "T:",true,true
WSHNetwork.RemoveNetworkDrive "Z:",true,true


For Each GroupObj In UserObj.Groups
    Select Case GroupObj.Name

        Case "group1"
            WSHNetwork.MapNetworkDrive "G:", "\\server\share1"
            WSHNetwork.MapNetworkDrive "H:", "\\server\share2"
	        WSHNetwork.MapNetworkDrive "S:", "\\server\share3"
            WSHNetwork.MapNetworkDrive "O:", "\\server\share4"

        Case "group2"
            WSHNetwork.MapNetworkDrive "G:", "\\server\share1"
            WSHNetwork.MapNetworkDrive "H:", "\\server\share2"
            WSHNetwork.MapNetworkDrive "O:", "\\server\share5"
            WSHNetwork.MapNetworkDrive "S:", "\\server\share7"
        
        Case "group3"
            WSHNetwork.MapNetworkDrive "R:", "\\server\share9"
    End Select
Next

WScript.Quit
 
Hi Rijay,

Sorry about this but what does the line mean.

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

I mean the line - & DomainString &
and - & UserString

 
Hello kachbo1,

[1] The UserString is exactly your strUserName. You can use your variable name at that place.

[2] DomainString is your netbios domain name. In the login script setup, you can retrieve it like this.
[tt]DomainString=ObjNetwork.UserDomain[/tt]

That's what I understand in regard to rijay's advice.

regards - tsuji
 
i would advise trying to move away from coding drive letters and group names into your vbscript.
the problem with this approach is that every time you want to change or add a drive mapping you have to modify your vbscript. everytime you modify it you need to go through testing it etc.

i would advise on writing a vbscript which reads an ini file of drive mappings.

you could have something like

group1=g:\\server\share1
group1%2=j:\\server2\shareA
group2=g:\\server\share3

that way once you have your script written and tested all you have to do is modify the ini file in the future.

regards,
richard
 
My apologizes I forgot two lines of code. Insert these 2 lines of code before the Set UserObj = ... line

Code:
DomainString = WSHNetwork.UserDomain
UserString = WSHNetwork.UserName

 
i know i am like a dog with a bone about this but i cant see the point of RemoveNetworkDrive.
i would advise you only to use RemoveNetworkDrive if you have checked that the drive letter in question is

1. already in use
2. NOT connected to the desired location already
(if it is already connected to the desired location then why bother disconnecting it only to have to reconnect it?)

regards
von moyla
 
I remove all previously mapped drives before mapping any drives for a reason. I have certain applications that will not run unless you "touch" the mapped drive prior to using it, removing the drive and remapping it accomplishes this.

MrMovie, do you have any sample code of your suggestions? Others may benefit from it if it is more efficient.
 
Public Function MapDrive(strDrvShare, blnPersistent, blnDisconnect)
On Error Resume Next
Dim blnDriveConnected
Dim blnAlreadyMapped
Dim strDrv
Dim strDrvLong
Dim strShare


blnAlreadyMapped = False
strDrv = Left(strDrvShare, 1)
strShare = Right(strDrvShare, Len(strDrvShare) - 2)
strDrvLong = strDrv & ":"
blnDriveConnected = FSO.DriveExists(strDrvLong)

If blnEnumDrives = False Then
Set enumDrives = WshNetwork.EnumNetworkDrives
blnEnumDrives = True
End If

If IsObject(enumDrives) Then
For i = 0 to enumDrives.Count -1
If enumDrives.Item(i) <> "" Then
If LCase(enumDrives.Item(i)) = LCase(strDrvLong) Then
'msgbox "found our drive already in use " & strDrvLong
If LCase(enumDrives.Item(i + 1)) = LCase(strShare) Then
'msgbox "found the share as well" & strShare
blnAlreadyMapped = True
End If
End If
End If
Next
End If

If blnAlreadyMapped = False Then
If FSO.FolderExists(strShare) Then
'msgbox "found that share " & strShare
Err.Clear
If blnDriveConnected = False Then
If blnDisconnect = "True" Then
'shouldnt need to disconnect here but just incase there is a persistant connection
'WshNetwork.RemoveNetworkDrive strDrvLong, True, True
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
If Err.Number = 0 Then
MapDrive = 0
blnEnumDrives = False
Else
'Msgbox Err.Number
'shouldnt need to disconnect here but just incase there is a persistant, remembered connection
WshNetwork.RemoveNetworkDrive strDrvLong, True, True
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
'msgbox "failed to map drive " & strDrvLong & strShare & " blPers " & blnPersistent
If Err.Number = 0 Then
MapDrive = 0
blnEnumDrives = False
Else
MapDrive = 4
End If
End If

Else
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
If Err.Number = 0 Then
MapDrive = 0
blnEnumDrives = False
Else
'msgbox "failed to map drive " & strDrvLong & strShare & " blPers " & blnPersistent
MapDrive = 4
End If
End If
ElseIf blnDriveConnected = True And blnDisconnect = "False" Then
'msgbox "cannot map drive " & strDrvLong & " already in use and disconnect set to false."
MapDrive = 3
ElseIf blnDriveConnected = True And blnDisconnect = "True" Then
WshNetwork.RemoveNetworkDrive strDrvLong, True, True
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
blnEnumDrives = False
If Err.Number = 0 Then
MapDrive = 0
Else
'msgbox "failed to map drive " & strDrvLong & strShare & " blPers " & blnPersistent
MapDrive = 2
End If
End If
Else
'msgbox "didnt find that share " & strShare & " " & strDrvLong
MapDrive = 1
End If
Else
MapDrive = 8 'already got it connected, might want to return 0
End If


End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top