Solved

VBScript Login Script

Posted on 2006-11-08
6
725 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
Comment Utility

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
Comment Utility
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
Comment Utility

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:rgonser
Comment Utility
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
Comment Utility

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
Comment Utility
thanks, ill see if that is more effective.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now