Network Login script advice

Hello,

Id like to create a network login script but would like some help on what the best method is. Is using VBS or using a batch file the best/most reliable method?

Many users already have manually mapped network drives and printers, so i need a script that can remove that particular mapped drive if it already exsists, so if the user currently has Letter O mapped to fileserver\users\bob and i need it changed to O fileserver2\everyone for example the script will do this.

If possible an example would be really helpful to map drives and printers, different users need different mapped drives and printers.

We are using Small business server 2003
kevin1983Asked:
Who is Participating?
 
mredfelixConnect With a Mentor Commented:
Hi it depends on all your level of coding. Vb will give you a nice graphicall interface like a splash screen while the commandline is simpler and less complicated to implement but you can do all you need to from a bat.

here is a good website for you to look at vb login script

http://www.computerperformance.co.uk/Logon/
0
 
AngelizedConnect With a Mentor Commented:
I would go also to VBS and ADSI, you will be able to dynamically add and remove Mapping for users.
by using also function like the one attach, you will be able to map and unmap drives.


Function MapDrive(strDrive, strPath, boolPersistent, strUser, strPassword)
   Set objNetwork = CreateObject("WScript.Network")
   If IsDriveMapped(strDrive) Then objNetwork.RemoveNetworkDrive strDrive
   On Error Resume Next
   If StrUser = "" Then 
      objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent
   Else   
      objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent, strUser, strPassword
   End If
   MapDrive = Err.Number
   Err.Clear
   
End Function
 
Function IsDriveMapped(strDrive)
    IsDriveMapped = False
    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_MappedLogicalDisk")
    For Each objItem In colItems
        If InStr(UCase(strDrive), objItem.Name) > 0 Then IsDriveMapped = True
    Next
End Function
 
Function UnMapDrive(strDrive)
   On Error Resume Next
   Set objNetwork = CreateObject("WScript.Network")
   If IsDriveMapped(strDrive) Then objNetwork.RemoveNetworkDrive strDrive
   If Err.Number = 0 Then
     UnMapDrive = True
   Else
     Err.Clear
     objNetwork.RemoveNetworkDrive strDrive, True
     If Err.Number = 0 Then
        UnMapDrive = True
      Else
          UnMapDrive = False
      End If
   End If
End Function

Open in new window

0
 
kevin1983Author Commented:
mredfelix: that site seems helpful

Angelized: would you be able to add a few comments so that i can understand the code better?, i can see the general idea of what this is doing, do you advise running one big script similar to this for all users? as i can see you have the line If StrUser = "" Then do you do a line for each user such as If StrUser = "bob" Then ..... If StrUser = "jane" Then?

Persume i need to add some code such as strDrive = "D:" strPath = "\\Fileserver\users"?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
kevin1983Author Commented:
Angelized: Somthing similar to below?
'VARIABLES
dim struser
 
strDrive = "O:"
strPath = "\\Fileserver\Users"
strPassword = "password"
 
 
Function MapDrive(strDrive, strPath, boolPersistent, strUser, strPassword)
   Set objNetwork = CreateObject("WScript.Network")
   If IsDriveMapped(strDrive) Then objNetwork.RemoveNetworkDrive strDrive
   On Error Resume Next
   If StrUser = "" Then 
      objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent
   Else   
      objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent, strUser, strPassword
   End If
   MapDrive = Err.Number
   Err.Clear
   
End Function
 
Function IsDriveMapped(strDrive)
    IsDriveMapped = False
    On Error Resume Next
 
'guess below lines are excuted on local computer, can you explain whats the below lines do?
 
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_MappedLogicalDisk")
    For Each objItem In colItems
        If InStr(UCase(strDrive), objItem.Name) > 0 Then IsDriveMapped = True
    Next
End Function
 
Function UnMapDrive(strDrive)
   On Error Resume Next
   Set objNetwork = CreateObject("WScript.Network")
   If IsDriveMapped(strDrive) Then objNetwork.RemoveNetworkDrive strDrive
   If Err.Number = 0 Then
     UnMapDrive = True
   Else
     Err.Clear
     objNetwork.RemoveNetworkDrive strDrive, True
     If Err.Number = 0 Then
        UnMapDrive = True
      Else
          UnMapDrive = False
      End If
   End If
End Function

Open in new window

0
 
AngelizedCommented:
Correct !
you can use the following syntax
MapDrive "L:","\\YOURSERVER\YOURSHARE",False,"", ""


0
 
mredfelixCommented:
try just mapping one drive first..start basic and build from there rather than test everything at once......You can place this in the \\server\netlogon and run it from there on your machine.
Option Explicit
Dim objNetwork 
Dim strDriveLetter, strRemotePath
strDriveLetter = "J:" 
strRemotePath = "\\Servername\sharename" 
 
' Purpose of script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method.  Result J: drive
Set objNetwork = CreateObject("WScript.Network") 
 
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath 
WScript.Quit

Open in new window

0
 
kevin1983Author Commented:
mredfelix: i created that script and it works ok

Angelized: not sure where you mean use this, not sure about some of the code, a few comments would help, do you mean
If StrUser = "Bob" Then
      objNetwork.MapNetworkDrive MapDrive "L:","\\YOURSERVER\YOURSHARE",False,"", "", boolPersistent
   Else  
?
Im guessting this code whenrun on the local computer finds the user thats loged into the network from Active directory and mappes a drive if that particular user is logged in?
0
 
AngelizedCommented:
no need of
If StrUser = "Bob" Then
    MapDrive "L:","\\YOURSERVER\YOURSHARE",False,"", "",
end if
0
 
mredfelixCommented:
well you want to test it removes the drive so add

objNetwork.RemoveNetworkDrive strDriveLetter

and printers etc use the site i gave you and build it up. the only way your learn is by making mistakes.
0
 
kevin1983Author Commented:
Ok thanks both for your help, i felt i had to split the points between you as both gave some good comments.

good point mredfelix
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.