Solved

VB Script for drive mapping

Posted on 2009-07-06
6
1,057 Views
Last Modified: 2012-06-27
Hi,
I am in the process of learning vb scripting but still not there yet to able to troubleshoot this VB script that suppose to delete existing drive mapping and then map fresh new drives. I am mapping drive to Windows 2003 and Windows 2008 server shares. Following are the errors\problems relates to this script. First of all I will confess that I did cut and paste couple of vb scripts from solutions presented at experts-exchange.com to find a working one for myself, so please bear with me when you go through it.

1. First, at this time I am running it on my PC as a test script by double clicking on it. It works intermittently. Sometimes it does not delete drives before mapping and sometimes I do not find all my drives.

2. I am mapping home drives using profiles in active directory. I like to know how I can map Home drive for individual using script.

3. Past weekend I ran a bigger test and use GPO to deliver mapping drives using this script. I saw my entire drives letters but there were missing server names along shares. E.g. instead of  Ch data on Server1 (Server1)' (L:) I was getting Disconnected drive (L:) even though they were working, but when I try to disconnect them I got drive path not found message.

I may be asking too many things here. Please ask me questions if you are not clear. These are the problem I need to resolve before I try again next weekend. Thanks in advance.


Best Regards
Iftikhar

' TwoMap.vbs - Map Network Drive to M: and P:

' Example of VBScript Mapping two drives in one script.

' Author Guy Thomas http://computerperformance.co.uk/

' Version 1.6 - April 24th 2005

' -----------------------------------------------------------------' 

Option Explicit

Dim objNetwork, strRemotePath1, strRemotePath2, strRemotePath3, strRemotePath4, strRemotePath5, strRemotePath6, strRemotePath7, strRemotePath8, colDrives,i
 

Dim strDriveLetter1, strDriveLetter2, strDriveLetter3, strDriveLetter4, strDriveLetter5, strDriveLetter6, strDriveLetter7, strDriveLetter8
 

strDriveLetter1 = "F:" 

strDriveLetter2 = "G:" 

strDriveLetter3 = "L:"

strDriveLetter4 = "P:"

strDriveLetter5 = "S:"
 
 

strRemotePath1 = "\\MainServer\APPS" 

strRemotePath2 = "\\MainServer\DATA" 

strRemotePath3 = "\\Server1\CH DATA"

strRemotePath4 = "\\Server1\Policies & Procedures"

strRemotePath5 = "\\MainServer\ITI"
 

Set objNetwork = CreateObject("WScript.Network")
 
 

Set colDrives = objNetwork.EnumNetworkDrives
 

For i = 0 to colDrives.Count-1 Step 2

      ' Force Removal of network drive and remove from user profile 

      ' objNetwork.RemoveNetworkDrive strName, [bForce], [bUpdateProfile]

      If UCase(colDrives.Item(i)) <> "H:" Then

            objNetwork.RemoveNetworkDrive colDrives.Item(i),TRUE,True

      End If
 

Next
 

' Section which maps two drives, M: and P:

objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1

objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3

objNetwork.MapNetworkDrive strDriveLetter4, strRemotePath4

objNetwork.MapNetworkDrive strDriveLetter5, strRemotePath5
 
 

Wscript.Quit
 

' End of Windows Logon Script Example

Open in new window

0
Comment
Question by:ihussain1306
6 Comments
 
LVL 6

Expert Comment

by:drewha1969
ID: 24788158
I know this doesnt really answer your question, but have you considered just making a simple batch file using the "net use" commands?

net use <driveletter>: /delete
net use <driveletter>: <path>

Just thought I would throw that out there since nobody has responded yet...
0
 

Author Comment

by:ihussain1306
ID: 24794034
Thanks for your comments. I have been using "Net Use" commands in my batch files for past few years without any problem. Recently I added Windows 2008 domain controller to my doamin and wanted to add shares by editing my existing bat file. However, i did not work and I always get this Error 1326 'Unknow username or bad password". vb scripts seems to be a solution for this error. If you know any other way that let me map drive to windows 2008 share using my bat files then I am willing to try. Thanks
0
 
LVL 1

Expert Comment

by:John_Miller
ID: 24794120
We have done away with login scripts altogether and instead have mapped drives using Group Policy Preferences. The nice thing about Group Policy Preference Drive Mappings is that they are persistent. This works well for travelling users as the drives will say Disconnected network drive, until they are back on the network via VPN. Then they can connect to the drive without launching a login script. This will also reduce the amount of time at login as it is done behind the scenes.

This has eliminated a ton of time in troubleshooting and we can base our drive mappings off of AD groups.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:ihussain1306
ID: 24807937
John, I appreciate your response. I have tried to use Group Policy Preference Drive Mappings but not able to make it work. Just to let you know our enviornment so may be then you can tell if this will work for me. I have mix versions of Domain controllers, four of them are Windows 2000 and one Main domain controller is 2003 and another one is 2008, so my Domain Functional Level is Windows 2000 Native and Forest Functional level is Windows 2000. Is there any chance that ican use Group Policy Preference Drive Mappings?

          I am still trying to get my vb script working. This is my 2nd script that I am working on. It does what it suppose to do when I run by double clcking it on my PC, but when I use the same Script using GPO; it give me drive mappings without Server and share name. e.g. it gives me "Disconnected Network drive (L:)" I can click on it to open this drive. Same thing happening to other drives too. Please someone look at my script and see what i am doing wrong. Thanks
Option Explicit

Dim objNetwork, oDrives,intDrive
 

Set objNetwork = CreateObject("Wscript.Network")

Set oDrives = objNetwork.EnumNetworkDrives 

 

	on error resume next

 

    If oDrives.Count > 0 Then 

        For intDrive = 0 to oDrives.Count-1 Step 2

            If (UCase(oDrives.Item(intDrive)) = "F:") Then

                objNetwork.RemoveNetworkDrive "F:" 

            ElseIf (UCase(oDrives.Item(intDrive)) = "G:") Then

                objNetwork.RemoveNetworkDrive "G:" 

            ElseIf (UCase(oDrives.Item(intDrive)) = "O:") Then

                objNetwork.RemoveNetworkDrive "O:" 

	    ElseIf (UCase(oDrives.Item(intDrive)) = "L:") Then

                objNetwork.RemoveNetworkDrive "L:" 

	    ElseIf (UCase(oDrives.Item(intDrive)) = "P:") Then

                objNetwork.RemoveNetworkDrive "P:"

	    ElseIf (UCase(oDrives.Item(intDrive)) = "S:") Then

                objNetwork.RemoveNetworkDrive "S:"

            End If

        Next 

    End If 
 
 
 

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "F:" , "\\MianServer\Apps"
 

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "G:" , "\\MainServer\Data"
 

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "O:" , "\\Server1\Ch Data"
 

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "L:" , "\\Server2\ChData"
 

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "P:" , "\\Server2\Policy"
 

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive "S:" , "\\MainServer\iti"
 

Wscript.Quit
 

' End of Windows Logon Script Example

Open in new window

0
 
LVL 10

Accepted Solution

by:
Chris_Gralike earned 250 total points
ID: 25230815
Here is an example that might help you out ;-)


Set Network = CreateObject("WScript.Network")

Set Objdomain = Getobject( "LDAP://rootDse")

Domainstr = Objdomain.get( "dnsHostName")

StrComputer = Network.computername
 

Set localDrives = network.EnumNetworkDrives

if LocalDrives.Count > 1 then

	For i = 0 to localDrives.Count -1 step 2

		On Error Resume Next

                'We are mapping H: using the domain profile so dont delete it here, or remove this statement'

		If Not localDrives.item(i) = "H:" then

			If Not localDrives.Item(i) = "" Then

				Network.RemoveNetworkDrive localDrives.Item(i), True, True

			End if

		end if

	Next

End If

'Create mappings based on security group memberships'

For Each GroupObj in UserObj.Groups

	On Error Resume Next

	Select Case GroupObj.Name

                'Echo the security group remove if you understand

                WScript.Echo GroupObj.Name

		case "DL SG LOC Finance"

			Network.MapNetworkDrive "G:", "\\UNC"

                        'If double mappings are made you might delete them here'

			Network.RemoveNetworkDrive "I:", True, True

			Network.MapNetworkDrive "I:", "\\UNC"

		case "DL SG LOC Corperate_ICT"

			Network.MapNetworkDrive "K:", "\\UNC"

	end select

Next
 
 

	

Open in new window

0
 

Author Closing Comment

by:ihussain1306
ID: 31600177
I have opened this question few months back and I decided to use bat files again even though I was looking for some help in my posted vb script.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Synchronize a new Active Directory domain with an existing Office 365 tenant
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now