Solved

VBScript Login Script

Posted on 2006-11-08
6
741 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 71

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 71

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
Industry Leaders: 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!

 
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 71

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

713 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