Solved

VBScript Login Script

Posted on 2006-11-08
6
732 Views
Last Modified: 2012-08-13
Hey Everyone,

I have a domain that I have recently wrote VBScript login scripts to replace batch ones. The problem is people started getting this "local device name is in use" error message. If I go in and manually remove the network drives before I log off and log on, the message goes away. However the default for MapNetworkDrive is supposed to be non-persistent connections? The server OS is Server 2003. Here is a basic format of the script.

Option Explicit
Dim WshNetwork, oDrives, strUName, i

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
 
    strUName = WshNetwork.UserName

DriveMapper "H:", "\\FileSrvG4\" & strUName & "$"
DriveMapper "F:", "\\FileSrvG4\Data",
DriveMapper "M:", "\\FileSrvG4\Images"
DriveMapper "P:", "\\FileSrvG4\Pricing"
DriveMapper "S:", "\\FileSrvG4\Sales"

Sub DriveMapper(Drive, Share)
For i = 0 to oDrives.Count -1 Step 2
if LCase(Drive) = LCase(oDrives.Item(i)) then
if not LCase(Share) = LCase(oDrives.Item(i+1)) then
WshNetwork.RemoveNetworkDrive Drive, true, true
Else
Exit Sub
End if
End if
Next
WshNetwork.MapNetworkDrive Drive, Share
End Sub


Thank you!
0
Comment
Question by:rgonser
  • 3
  • 3
6 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 400 total points
ID: 17908259

That one has always been a bit odd. It's only supposed to update the profile information if you set a specific (and as you say, non-default) flag in the method call. I never managed to figure out why it's usage is unreliable though, so this may not be very helpful.

There are a few ways you can get around it. You can ignore it and carry on like this:


Sub DriveMapper(Drive, Share)
      For i = 0 to oDrives.Count -1 Step 2
            if LCase(Drive) = LCase(oDrives.Item(i)) then
                  if LCase(Share) <> LCase(oDrives.Item(i+1)) then
                        WshNetwork.RemoveNetworkDrive Drive, true, true
                  Else
                        Exit Sub
                  End if
            End if
      Next
      On Error Resume Next
      WshNetwork.MapNetworkDrive Drive, Share
      On Error Goto 0
End Sub


Or you can try and avoid it like this:


Set objFileSystem = CreateObject("Scripting.FileSystemObject")

Sub DriveMapper(Drive, Share)
      For i = 0 to oDrives.Count -1 Step 2
            if LCase(Drive) = LCase(oDrives.Item(i)) then
                  if LCase(Share) <> LCase(oDrives.Item(i+1)) then
                        WshNetwork.RemoveNetworkDrive Drive, true, true
                  Else
                        Exit Sub
                  End if
            End if
      Next
      If Not objFileSystem.DriveExists(Drive) Then
            WshNetwork.MapNetworkDrive Drive, Share
      End If
End Sub


You could also disconnect and reconnect the drive regardless of what it is as well if you wanted to.

HTH

Chris
0
 
LVL 3

Author Comment

by:rgonser
ID: 17908580
Yeah, is that a common issue?

But thank you Chris, A+, very helpful, and thanks for the quick reply!
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 17908671

We suffer from it here too, it's not all that predictable unfortunately which has always been a little odd. I suspect it's leaving something lingering in the profile which is causing it to remain persistent.

Our drive mapping routine is not too dissimilar from yours, the only difference these days is that it removes the existing mapping (even if it's the same one) and forces an update to the profile with the new mapping.

Chris
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 3

Author Comment

by:rgonser
ID: 17913798
Could you show how I'd write a Sub that does a forced removal and remaps, because we started getting the error again today?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 17914517

Sure, this is ours minus the logging bits:


Sub MapDrive(strDrive, strShare)
      Dim objFileSystem, objDrive, objNetwork

      Set objFileSystem = CreateObject("Scripting.FileSystemObject")
      If objFileSystem.DriveExists(strDrive) Then
            Set objDrive = objFileSystem.GetDrive(strDrive)
            If objDrive.DriveType <> 3 Then
                  ' Fixed Drive
                  Exit Sub
            End If
      End If
      Set objFileSystem = Nothing
      Set objNetwork = CreateObject("WScript.Network")
      On Error Resume Next
      objNetwork.RemoveNetworkDrive strDrive, False, True
      objNetwork.MapNetworkDrive strDrive, strShare, True
      On Error Goto 0
      Set objNetwork = Nothing
End Sub

0
 
LVL 3

Author Comment

by:rgonser
ID: 17914589
thanks, ill see if that is more effective.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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