• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1226
  • Last Modified:

Login script - unmap drives not working reliably

Hi,
i have a network login script (vb script) to map some network drives, the problem is the script doesnt correctly remove drives that are already mapped, for example if the code is run on a computer and on that pc drive X is already mapped to \\fileserver\sharedfolder the script shows drive X as disconnected in my computer but doesnt remap the drive to \\Fileserver\Accounts, if however drive x is not mapped to any drives on that pc then the script will correctly map drive x to \\Fileserver\Accounts.

Also if drive Z is mapped to any drives on the pc the the script should remove that mapped drive but not map a new path to that drive but again it doesnt disconnect the mapped drive.

So the code maps drives ok but doesnt unmap correctly.
Any help on how i can modify the code to ensure i can unmap drives reliably would be great?


Option Explicit
Dim objNetwork, objShell, wshshell, CheckDrive, DriveLetterX, DriveLetterY
Dim RemotePathX, RemotePathY
 
Set objNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell") 
Set CheckDrive = objNetwork.EnumNetworkDrives() 
 
DriveLetterX = "X:"
DriveLetterY = "Y:"
 
 
RemotePathX = "\\Fileserver\Accounts" 'Network Paths
RemotePathY = "\\Fileserver\Projects" 
 
 
 
On error resume next
 
 
 '******************** X mapped drive *********************
 
' This section tests to see if the Drive is already mapped, 
AlreadyConnected = False  
For i = 0 To CheckDrive.Count - 1 Step 2 
If CheckDrive.Item(i) = DriveLetterX Then AlreadyConnected = True 
Next 
 
'and if drive connected is yes then disconnect drive
 
If AlreadyConnected = True then 
objNetwork.RemoveNetworkDrive DriveLetterX 
objNetwork.MapNetworkDrive DriveLetterX, RemotePathX
 
'Map drive 
Else
objNetwork.MapNetworkDrive DriveLetterX, RemotePathX
end if
 
 
'******************** Y mapped drive *********************
 
' This section tests to see if the Drive is already mapped, 
AlreadyConnected = False  
For i = 0 To CheckDrive.Count - 1 Step 2 
If CheckDrive.Item(i) = DriveLetterY Then AlreadyConnected = True 
Next 
 
'and if drive connected is yes then disconnect drive
 
If AlreadyConnected = True then 
objNetwork.RemoveNetworkDrive DriveLetterY 
objNetwork.MapNetworkDrive DriveLetterY, RemotePathY
 
'Map drive 
Else
objNetwork.MapNetworkDrive DriveLetterY, RemotePathY
end if
 
 
'******************** Z mapped drive *********************
 
AlreadyConnected = False  
For i = 0 To CheckDrive.Count - 1 Step 2 
If CheckDrive.Item(i) = DriveLetterZ Then AlreadyConnected = True 
Next 
 
 
If AlreadyConnected = True then 
objNetwork.RemoveNetworkDrive DriveLetterZ 
objNetwork.MapNetworkDrive DriveLetterZ, RemotePathZ

Open in new window

0
kevin1983
Asked:
kevin1983
  • 11
  • 10
1 Solution
 
Briguy_572Commented:
Kevin,
try this  - you will need to rename it to a .vbs file.
there are commented out wscript.echo lines if you need to debug.  the numbers in "()" are the line numbers of the script.
good luck.
mapXY-rename-txt.txt
0
 
kevin1983Author Commented:
Thanks for reply,

ive tried this, the script runs fine but doesnt seem to remove drive X and Y if they are already mapped, it does however seen to remove drive Z fine. the scirpt maps X and Y fine if the drive letters are not currently mapped

Any suggestions?
0
 
Briguy_572Commented:
kevin- it is removing them and remapping them.  if you are running the script manually (not during logon), it remaps the drive, but the display name in Explorer does not reflect the new share.  If you look at the contents of the X and Y after they are remapped, you should see that they have been remapped - explorer is just showing the old name.  if the script is run during logon, the correct display name will show in Explorer- Windows doesnt seem to like having the the shares remapped on the fly.
take a look at what is in the X and Y folders - it should be what you want for X and Y.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Briguy_572Commented:
line 44 is removing the share if it is there, then line 45 remaps it.
0
 
kevin1983Author Commented:
Hi, Ive set the script in group policy to run during login and its mapping drives that are not already mapped fine but drives that have already been manaully mapped are not being removed and re-mapped they are just left as mapped to the exsisting drive letter.

any ideas how to fix this?
0
 
Briguy_572Commented:
they are network drives, and not local drives (disks or CD ROM, etc)?
Can the user manually "disconnect" the drive?
0
 
kevin1983Author Commented:
Yes they are network drives if thats what you mean and not cd-rom,floppy drives, usb drives ect.

yes user can manaully disconnect them fine, the scipt is mapping drives fine, just any drives that have already been manually mapped are not being removed and re-mapped.

The Script is running in group policy in the login script section.
0
 
Briguy_572Commented:
Kevin -
i added  a few more wscript.echo lines to this one - try running this from the command prompt on one of the machines that is not re-mapping.
(run it as "cscript.exe mapXY-rename-txt-01.vbs").
if there are any errors - let me know.
Also- do you have an example of the UNC pathfor one of the drives that is not disconnecting (\\server\share)?
mapXY-rename-txt-01.txt
0
 
kevin1983Author Commented:
ok i run it from a command promt and seemed to work that time (no errors), i used the following paths in the script, origianally this drive letters where manually mapped to diffrent network paths.

Ill try a few more tests to make sure its ok, what did you change in the script?

RemotePathV = "\\Falcon\Director"
RemotePathW = "\\Falcon\Projects"
RemotePathX = "\\Falcon\Complaints"
RemotePathY = "\\Falcon\Accounts"
RemotePathZ = "\\Falcon\Admin"
0
 
Briguy_572Commented:
i just added a few lines of wscript.echo to show some output - you wont want that if it is running as the logon script.    So you added a few other drives to map?  the code i wrote was only for X & Y - the attached file will map these drives (and disconnect/remap any drives that are using those letters).
RemotePathV = "\\Falcon\Director"
RemotePathW = "\\Falcon\Projects"
RemotePathX = "\\Falcon\Complaints"
RemotePathY = "\\Falcon\Accounts"
RemotePathZ = "\\Falcon\Admin"
i had also seen that you wanted
0
 
Briguy_572Commented:
here is the file

mapXY-rename-txt-02.txt
0
 
kevin1983Author Commented:
Yes I added more drives to map and i added the extra lines of code needed like youve just done, but it still doesnt seem to want to unmap and remap drives that have already been mapped when the script is run at login.

See code below i have now
'script to map drives.  also remove z drive if it exists.
'	
'==========================================================================
 
Option Explicit
Dim DriveLetterA, DriveLetterB, DriveLetterC, DriveLetterD, DriveLetterE, DriveLetterF, DriveLetterG, DriveLetterH, DriveLetterI, DriveLetterJ, DriveLetterK, DriveLetterL, DriveLetterM, DriveLetterN, DriveLetterO, DriveLetterP, DriveLetterQ, DriveLetterR, DriveLetterS, DriveLetterT, DriveLetterU, DriveLetterV, DriveLetterW, DriveLetterX, DriveLetterY, DriveLetterZ 
Dim RemotePathA, RemotePathB, RemotePathC, RemotePathD, RemotePathE, RemotePathF, RemotePathG, RemotePathH, RemotePathI, RemotePathJ, RemotePathK, RemotePathL, RemotePathM, RemotePathN, RemotePathO, RemotePathP, RemotePathQ, RemotePathR, RemotePathS, RemotePathT, RemotePathU, RemotePathV, RemotePathW, RemotePathX, RemotePathY, RemotePathZ
Dim objFSO, objNetwork
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
 
DriveLetterE = "E:" 
DriveLetterF = "F:"
DriveLetterG = "G:"
DriveLetterH = "H:"
DriveLetterI = "I:"
DriveLetterJ = "J:"
DriveLetterK = "K:"
DriveLetterL = "L:"
DriveLetterM = "M:"
DriveLetterN = "N:"
DriveLetterO = "O:"
DriveLetterP = "P:"
DriveLetterQ = "Q:"
DriveLetterR = "R:"
DriveLetterS = "S:"
DriveLetterT = "T:"
DriveLetterU = "U:"
DriveLetterV = "V:"
DriveLetterW = "W:"
DriveLetterX = "X:"
DriveLetterY = "Y:"
DriveLetterZ = "Z:"
 
 
 
RemotePathE = "\\Falcon\Project Files" 'Network Paths
RemotePathF = "\\Icon360\Ac_Data" 
RemotePathG = "\\icon360\P_paysolve" 
RemotePathH = "\\icon360\Icon_direct" 
RemotePathI = "\\icon360\Impromptu" 
RemotePathJ = "\\icon360\Bacctl" 
RemotePathK = "\\falcon\Complaints" 
RemotePathL = "\\Falcon\Lloydslink" 
RemotePathM = "\\Falcon\Market" 
RemotePathN = "\\falcon\CIP" 
RemotePathO = "\\Falcon\Kevin" 
RemotePathP = "\\Falcon\Prod" 
RemotePathQ = "\\Falcon\Quality" 
RemotePathR = "\\Falcon\Recep" 
RemotePathS = "\\Buffalo\Everyone" 
RemotePathT = "\\falcon\Transfer" 
RemotePathU = "\\icon360\Ac_Source" 
RemotePathV = "\\Falcon\Director" 
RemotePathW = "\\Falcon\Projects" 
RemotePathX = "\\Falcon\Complaints" 
RemotePathY = "\\Falcon\Accounts" 
 
 
'----call DriveMapper sub
DriveMapper DriveLetterE, RemotePathE
DriveMapper DriveLetterF, RemotePathF
DriveMapper DriveLetterG, RemotePathG
DriveMapper DriveLetterH, RemotePathH
DriveMapper DriveLetterI, RemotePathI
DriveMapper DriveLetterJ, RemotePathJ
DriveMapper DriveLetterK, RemotePathK
DriveMapper DriveLetterL, RemotePathL
DriveMapper DriveLetterM, RemotePathM
DriveMapper DriveLetterN, RemotePathN
DriveMapper DriveLetterO, RemotePathO
DriveMapper DriveLetterP, RemotePathP
DriveMapper DriveLetterQ, RemotePathQ
DriveMapper DriveLetterR, RemotePathR
DriveMapper DriveLetterS, RemotePathS
DriveMapper DriveLetterT, RemotePathT
DriveMapper DriveLetterU, RemotePathU
DriveMapper DriveLetterV, RemotePathV
DriveMapper DriveLetterW, RemotePathW
DriveMapper DriveLetterX, RemotePathX 
DriveMapper DriveLetterY, RemotePathY 
 
 
'----Z drive piece ----
If objFSO.DriveExists("z:") Then 'If the drive already exists, then...
	'wscript.echo vbtab & "(24) drive Z exists - remove it"
	objNetwork.RemoveNetworkDrive "z:", True, True
Else
	'wscript.echo vbtab & "(27) there is no drive Z"
End If
 
WScript.Quit
 
 
 
 '-------------------------------------------------------------------------------------------
Sub DriveMapper(Drive, Share)  'Do the actual mapping of drives- 
	'wscript.echo vbTab & "(35) Calling DriveMapper Sub: " & drive & ", " & share
	Dim  d
	'DriveType "3" is a Network Drive
   	If objFSO.DriveExists(drive) Then 'If the drive already exists, then...
   	'wscript.echo vbtab & vbtab & "(39) Drive exists: " & drive
   	   	Set d = objFSO.GetDrive(objFSO.GetAbsolutePathName(drive))
		If d.drivetype = "3" Then 'If it is a network drive
			If lcase(d.sharename) <> LCase(share) Then 'Shares are mapped to different locations
				'Remap the drive to the share we want
				objNetwork.RemoveNetworkDrive drive, True, True
				objNetwork.MapNetworkDrive Drive, Share, False 'True makes it persistant
				Exit Sub 
			End If
		Else
			'wscript.echo "(49) It is not a network drive"
		End If
	Else 'Drive is not there- map it
		'wscript.echo vbTab & "(52) Map the drive: " & drive & " " & share 
		objNetwork.MapNetworkDrive Drive, Share, False 'True makes it persistant
	End If
	Set d = Nothing 
	'wscript.echo vbTab & vbTab & "(56) Ending Drive Mapper Sub: " & share
End Sub
'-------------------------------------------------------------------------------------------

Open in new window

0
 
kevin1983Author Commented:
Scirpt is se as login script in group policy on SBS 2003, see screenshot
login.JPG
0
 
Briguy_572Commented:
i think i see the problem.  you are running this as a "machine startup script" - these typically apply to computer related settings - these are applied when the computer boots up.
you want to apply the logon script under "User Configuration/Windows Settings/Scripts/Logon".  These apply when the user logs on.  Network folders are profile specific.  Try applying the script there..
You may also want to try downloading the Group Policy Management Console (http://www.microsoft.com/downloads/details.aspx?familyid=0a6d4c24-8cbd-4b35-9272-dd3cbfc81887&displaylang=en) - it is a much better tool for viewing/editing Group policies - especially the "settings" tab.
Please see the attached files for where a user logon script should go.
gpmc-1-user-logonscript-location.jpg
gpmc-2-settings-tab.jpg
0
 
Briguy_572Commented:
Kev- that is a lot of mapped drives as well... also, you must get a lot of complaints - since both the K and X drives are mapped there.  ;-)
0
 
kevin1983Author Commented:
Hi, sorry about late reply, thanks for further comments, ok i changed the script to run under "User Configuration/Windows Settings/Scripts/Logon".  Im sure i originally had the scirpt running there but at some point removed it from there an accidently run it under the "machine startup script" instead.

The situation now is the script is removing drive x fine now if it exsists and also removing and remapping drives if they have already been mapped. However the only last issue is when i go into my computer the drives mapped directory names are not being updated. For example if drive Q was manually mapped to \\falcon\project and i log of and back on and the login script runs removing Q and re-mapping Q to \\falcon\Quality i then go into my computer and find that Q is still showing as "Project on Falcon" instead of "Quality on Falcon" however if i double click on Q to got into the mapped drive all the files and folders displayed correspond to the files in Quality and not project.

So it seems to unmap/map but not the drive lable is not refreshing correctly in my computer even if i click the refresh button. This is perhaps fairly minor but this could easily confuse some users.

:-) well spotted, not to many i hope. Ok i see i dont need to drives mapped to complaints ill fix that, yes its a lot of mapped drives but this is just for testing, the idea is to have consitant mapped drives to the same drive letter accross the company. not all users need all mapped drives of course, just testing alll drives.

Any suggestion if its possible to fix this?
0
 
Briguy_572Commented:
Kevin,
Try inserting this line of code into the "DriveMapper" sub function - it will basically pause the script for a second IF the drive is mapped to something else, and then re-map it.  that seems to fix the display issue on my machine.
WScript.sleep 1000
example:

objNetwork.RemoveNetworkDrive drive, True, True
WScript.sleep 1000
objNetwork.MapNetworkDrive Drive, Share, False 'True makes it persistant

Open in new window

0
 
kevin1983Author Commented:
Ok i made that change, 2 drives that were manaully mapped and then removed/remapped by the script were done correctly and now displays there label status correctly in my computer but 1 drive label in my computer did not display correctly (but was remapped correctly).

So it seems its almost there but not quite - that line of code has helped but not quite fixed the issue, maybe because im mapping lots of drives?, maybe  i should try increasing the sleep time to 2000?.

Interestingly the scirpt does not run if i double cllick on the file on my computer (invalid procedure call or argument message)  but runs ok through group policy.

Is it best to map drives persistantly? as i noticed the comment you put in the code, and its set to non persistant, which is best?
0
 
Briguy_572Commented:
i think the persistent thing is a preference issue.  i had written the code to use persistent mappings, but a co-worker wanted them to not be persistent - I think he was just making a comment since he had not written the code and we were going over it in a dept meeting.  either way.
try 2000 - i just set it to a 1000 and it worked on mine.
also- I have a versio of this script that will map drives based on Active directory group mappings.  if you want that one, create a new question and post the name of it into this ticket.
0
 
kevin1983Author Commented:
Hm ok ill leave as non persistant for now, 2000 didnt seem to work and took to long to map the drives, ive changed it back to 1000, it works quite reliably overall now.

Ok, many thanks for your help
0
 
kevin1983Author Commented:
ok, many thanks for all your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now