Solved

VBScript Login Script

Posted on 2006-11-08
6
739 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
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.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

828 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