How to map a drives under VBS with different logon credentials

As not to go off topic with this post I have removed most of everything else in this script apart from anything relating to drive mappings.

Please note I am no expert in scripting and the information below should be properly tested if you intend to use it.

The script below maps drives for uses in two ways. First off, it will give all users the default shares and there home drive (P, S, X).

Then it checks what groups a user is a member of and attaches the appropriate drives. (W, U, Z, M, L)

Drives M, L attach to another server and also use different logon credentials. If you required another drive with different credentials, you could try…

On the line

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path, objNetwork, objNetwork1

Add another value “objNetwork2”

Then find in the script below and copy and paste it to a new section. Then replace all the 1’s with 2’s.

Amend the username, password and the share to reflect the desired connection. And fingers crossed it should work.

Set objNetwork1 = WScript.CreateObject(”WScript.Network”)
strLocalDrive1 = “L:”
strRemoteShare1 = “\\server\share”
strPer1 = “FALSE”
strUsr1 = “username”
strPas1 = “password”
objNetwork1.MapNetworkDrive strLocalDrive1, strRemoteShare1, strPer1, strUsr1, strPas1

‘ —— Script START ——

ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path, objNetwork, objNetwork1

Set WSHShell = CreateObject(”WScript.Shell”)
Set WSHNetwork = CreateObject(”WScript.Network”)

‘Automatically find the domain name
Set objDomain = getObject(”LDAP://rootDse”)
DomainString = objDomain.Get(”dnsHostName”)
WinDir = WshShell.ExpandEnvironmentStrings(”%WinDir%”)

‘Grab the user name
UserString = WSHNetwork.UserName

‘Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject(”WinNT://” & DomainString & “/” & UserString)

‘Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName

‘Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next

‘Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 300

‘Map drives
‘Note the first command uses the user name as a variable to map to a user share.

WSHNetwork.MapNetworkDrive “p:”, “\\server\users\” & UserString,True
WSHNetwork.MapNetworkDrive “s:”, “\\server\standards”,True
WSHNetwork.MapNetworkDrive “X:”, “\\server\company”,True

‘Now check for group memberships and map appropriate drives

For Each GroupObj In UserObj.Groups
Select Case GroupObj.Name
‘Check for group memberships and take needed action
Case “grp-Admin_tools”
WSHNetwork.MapNetworkDrive “w:”, “\\server\share”,True
WSHNetwork.MapNetworkDrive “u:”, “\\server\share”,True

Case “grp-temp-user”
WSHNetwork.MapNetworkDrive “w:”, “\\Server\Share”,True
Case “grp-FTP-root-rw”
Set objNetwork = WScript.CreateObject(”WScript.Network”)
strLocalDrive = “M:”
strRemoteShare = “\\server\share”
strPer = “FALSE”
strUsr = “username”
strPas = “password”
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer, strUsr, strPas

Case “grp-ftp-root-r”
Set objNetwork1 = WScript.CreateObject(”WScript.Network”)
strLocalDrive1 = “L:”
strRemoteShare1 = “\\server\share”
strPer1 = “FALSE”
strUsr1 = “username”
strPas1 = “password”
objNetwork1.MapNetworkDrive strLocalDrive1, strRemoteShare1, strPer1, strUsr1, strPas1

End Select

Next

‘=====================================
‘Add On Code goes above this line

‘Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing
Set objNetwork = Nothing
Set objNetwork1 = Nothing

‘Quit the Script
wscript.quit

‘ —— Script END ——

0 Responses to “How to map a drives under VBS with different logon credentials”


  1. No Comments

Leave a Reply

You must login to post a comment.