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

Problem with mapping drives in VBS login script.

I have been working on login scripts for use in our company that will always map the same 3 drives.  There is an individual script for each location which is assigned to each group of users in a location by group policy.  The script seems to work fine if your working with a blank slate, meaning that there are no other drives that were previously mapped.  When the script encounters an old drive mapping it is supposed to disconnect that mapping and then map the new drive(which could be the same but for testing purposes it has always been different.)

My problem occurs when I try and run another locations script to map their drives I am finding that the old drive mappings will stay and the new ones will never map.  This is a fairly inconsistent problem as I was able to test the script without any problem on my computer after I raised the sleep period to 1000(it was at 300.)  This same script will not work on another machine I am testing with unless I raise the sleep period to 10000.  
My question is:  Is their any problem with the code or is this problem only related to the timing of my sleep command?  Is there a way to make the code more efficient so it does not need those sleep commands?  This is one of my first login scripts using VBS so I apologize if I am missing something fairly easy.

The code is as follows: (some code unrelated to drive mapping has been omitted)

'==================
'BEGIN
'==================


ON ERROR RESUME NEXT 'ignore errors

'==========================================================
'Clean up old drive mappings
'==========================================================

Dim objNetwork, strDriveLetter
Dim bForce
bForce = True
strDriveLetter = "F:"
strDriveLetter2 = "N:"
strDriveLetter3 = "O:"
strDriveLetter4 = "T:"
strDriveLetter5 = "S:"
strDriveLetter6 = "I:"
strDriveLetter7 = "L:"

Set objNetwork = CreateObject("WScript.Network")

' Section which removes strDriveLetter
objNetwork.RemoveNetworkDrive strDriveLetter, _
bforce
objNetwork.RemoveNetworkDrive strDriveLetter2, _
bforce
objNetwork.RemoveNetworkDrive strDriveLetter3, _
bforce
objNetwork.RemoveNetworkDrive strDriveLetter4, _
bforce
objNetwork.RemoveNetworkDrive strDriveLetter5, _
bforce
objNetwork.RemoveNetworkDrive strDriveLetter6, _
bforce
objNetwork.RemoveNetworkDrive strDriveLetter7, _
bforce

wscript.sleep 1000

'==========================================================
'End Clean up old drive mappings.
'==========================================================


'==========================================================
'Standard Drive Map
'==========================================================
Set objNetwork = CreateObject("WScript.Network")

'Section which maps three drives, F:, N:, and O:
objNetwork.MapNetworkDrive "F:", "\\FS\DATA"
objNetwork.MapNetworkDrive "N:", "\\FS\CompanyShare"
objNetwork.MapNetworkDrive "O:", "\\FS\LocationShare"

'==========================================================
'End Standard Drive Map
'==========================================================

Any help is greatly appreciated.

Thanks
0
LSB-IT
Asked:
LSB-IT
  • 3
  • 2
1 Solution
 
RobSampsonCommented:
Hi, I'm not sure if this will actually fix your problem, but to avoid using "On Error Resume Next", I have added an objFSO to check if the drive exists before removing it.  Other than that, not much has changed....
'============
'==================
'BEGIN
'==================


'ON ERROR RESUME NEXT 'ignore errors

'==========================================================
'Clean up old drive mappings
'==========================================================

Dim objNetwork, strDriveLetter, objFSO
Dim bForce
bForce = True
arrDrives = Array("F:", "N:", "O:", "T:", "S:", "I:", "L:")

Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Section which removes strDriveLetter
For Each strDriveLetter In arrDrives
      If objFSO.DriveExists(strDriveLetter) = True Then _
            objNetwork.RemoveNetworkDrive strDriveLetter, bForce
Next

wscript.sleep 1000

'==========================================================
'End Clean up old drive mappings.
'==========================================================


'==========================================================
'Standard Drive Map
'==========================================================
Set objNetwork = CreateObject("WScript.Network")

'Section which maps three drives, F:, N:, and O:
objNetwork.MapNetworkDrive "F:", "\\FS\DATA"
objNetwork.MapNetworkDrive "N:", "\\FS\CompanyShare"
objNetwork.MapNetworkDrive "O:", "\\FS\LocationShare"

'==========================================================
'End Standard Drive Map
'==========================================================
'============

Regards,

Rob.
0
 
monvelasquezCommented:
Could it be that the other user doesn't have permissions to view the shared folders --  "\\FS\DATA", "\\FS\CompanyShare", "O:", "\\FS\LocationShare" ?
0
 
LSB-ITAuthor Commented:
I tried what you posted Rob and at first it didn't work but I placed a "wscript.sleep" command inside the for loop just after the remove network drive command to slowdown the script.  I had to play around with the time given to the sleep command so that it would work on every PC I run this script on.  So far I found that "wscript.sleep 1000" seems to work the best  I also added the extra command of updating the profile in the remove network drive command, this seemed to fix a problem that when some drives were mapped manually with the "map at logon" box checked they would not be cleared out when the script ran.  I have had luck with this and it seems to work on a couple PC's I have tested this on but not all of them.  It will work on mine and another co-workers but will not work on another PC I have tried.  On one of the PCs it mapped a completely random directory.  That one totally confused me.  I am also sure that this user has access to these shares as I have tested this manually without using the script.

Am I approaching this the right way?  Should my login scripts be using VBScript?  I was sure that this is what most people use for their login scripts but I have had so many problems getting this mapping part of the script to work I am second guessing myself here.  Does anyone have any other suggestions on how to get this to work or a better way to approach it?

Your help is appreciated.

P.S.  Thanks Rob for correcting my sloppy code with the For loop and the array.  Much cleaner now :)
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
RobSampsonCommented:
Hi, I think this seems to be the only way to do it properly.  Everything I search for comes up with having used WScript.Sleep as well, stating "give the PC time to do the disconnect".

The only thing that I can think of is to actually use the DOS command "net use" to do the deletion, and possibly the mapping if you wanted to.  This means that you can use the Shell's Exec method, and watch the Status to wait for it to finish, but I'm not sure if that actually wait for the process of the Net Use command to finish, or if more stuff happens in the background even after the Net Use command has finished.  Something like this:
'==================
'BEGIN
'==================


'ON ERROR RESUME NEXT 'ignore errors

'==========================================================
'Clean up old drive mappings
'==========================================================

Dim objNetwork, objFSO, objShell, objExec
Dim bForce, strCommand
Dim strDriveLetter, arrDrives, strDriveMap, arrMappings
bForce = True
arrDrives = Array("F:", "N:", "O:", "T:", "S:", "I:", "L:")

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Section which removes strDriveLetter
For Each strDriveLetter In arrDrives
      If objFSO.DriveExists(strDriveLetter) = True Then
            strCommand = "net use " & strDriveLetter & " /DELETE"
            'MsgBox strCommand
            Set objExec = objShell.Exec(strCommand)
            While objExec.Status = 0
                  ' Wait for the command to finish
            Wend
            Set objExec = Nothing
      End If
Next

'==========================================================
'End Clean up old drive mappings.
'==========================================================


'==========================================================
'Standard Drive Map
'==========================================================
Set objNetwork = CreateObject("WScript.Network")

'Section which maps three drives, F:, N:, and O:
arrMappings = Array("F:~\\FS\DATA","N:~\\FS\CompanyShare", "O:~\\FS\LocationShare")

For Each strDriveMap In arrMappings
      strCommand = "net use " & Split(strDriveMap, "~")(0) & " " & Split(strDriveMap, "~")(1) & " /PERSISTENT:NO"
      'MsgBox strCommand
      Set objExec = objShell.Exec(strCommand)
      While objExec.Status = 0
            ' Wait for the command to finish
      Wend
      Set objExec = Nothing
Next

'==========================================================
'End Standard Drive Map
'==========================================================

Regards,

Rob.
0
 
LSB-ITAuthor Commented:
The DOS shell commands worked better for me.  I had to tweak the sleep command a little to get the right timing and make sure it worked on different systems.  It really depends on how fast the particular PC can perform the removal of all old drives.  Some of them could do it easily without a sleep command but other older PCs or *slower* PCs I should say wouldn't disconnect the old drives and would not when mapping, map over the old drives either.  Needless to say I appreciate your help and sorry for waiting so long to give you the points Rob.  I was going to test it on a small part of the production network to see the more widespread affect of it and everything is going well so I am satisfied.

Thanks Again.
0
 
RobSampsonCommented:
I am surprised you still had to use the Sleep commands, even with the
      While objExec.Status = 0
            ' Wait for the command to finish
      Wend

bits in it.  I guess that means it doesn't truly wait for the command to finish.....

Rob.
0

Featured Post

Technology Partners: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now