Solved

VBS Network Login Script

Posted on 2006-06-28
5
276 Views
Last Modified: 2010-04-30
I am using the following code to create my login script that will map network drives.  However, I am having a few problems.

1. In Active directory I have login script path pointing to the login.vbs,  The problem is everytime I login, I get an error the drive is already in use.  Is there an overwrite command?  I dont want to disconnect all because some users may have their own manually mapped drives.  I suppose I could also just disconnect the drives the drives that are about to be mapped again.  Just not sure what is the best way or how to do it.

2.  Is it possible to prevent the name of the mapped drive from being stored in the users profile or wherever it is stored?  If I try to the net use command on an existing letter I get the renamed version from the script I had run instead of "share on server."

3.  Is there away to do a "net time \\servername /set /y"

Here is my what I have:

Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName


strDriveLetter = "H:"
strRemotePath = "\\server\share1"
strNewName = "Share Name 1"

strDriveLetter = "I:"
strRemotePath = "\\server\share1"
strNewName = "Share Name 1"

strDriveLetter = "J:"
strRemotePath = "\\server\share3"
strNewName = "Share Name 3"


Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath,,Username,Password


Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

WScript.Quit
0
Comment
Question by:ohmErnie
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 17001555

Hi there,

1. Not as such. You can approach the problem a number of different ways.

 - Supress the error completely by adding "On Error Resume Next" into your script

On Error Resume Next
objNetwork.RemoveNetworkDrive strDriveLetter, False, True
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath,,Username,Password

 - Check and see if the drive is mapped before trying to map it:

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
If objFileSystem.DriveExists("H:") Then
    objNetwork.RemoveNetworkDrive strDriveLetter, False, True
End If
strDriveLetter = "H:"
strRemotePath = "\\server\share1"
strNewName = "Share Name 1"
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath,,Username,Password


The rest will follow in a moment.

Chris
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 17001601

As far as the rest of drive mapping goes I use a slightly modified version of the subroutine below to handle ours. This means each mapping can be added with:

MapDrive "H:", "\\Server\Home\User"

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
                  ' Drive Type is not a Network 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


2. That's an issue with storing it in the users profile, I'm afraid I don't know of a fix for it.


3. Yep, nice and easy:

Set objShell = CreateObject("WScript.Shell")
objShell.Run "net time \\servername /set /y"


Chris
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

776 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