Solved

VBS Network Login Script

Posted on 2006-06-28
5
273 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

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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 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…

706 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

20 Experts available now in Live!

Get 1:1 Help Now