?
Solved

VBScript Login Script

Posted on 2006-11-08
6
Medium Priority
?
752 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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