Solved

Log on scripts Windows 2003 Server

Posted on 2004-10-14
6
263 Views
Last Modified: 2010-04-19
Hello Experts,

We are running windows 2003 server and I want to create a default log in script the script that I have been working on is giving me trouble. The things that I want to do is when a user logs on I want some of the drives to be mapped to all the users and some drives to be mapped if they belong to a specific group. Before mapping the drive I want to check if that drive is already mapped if mapped I want to remove that mapping and create a new mapping. For example “Contact list” should be mapped to all the users in the network and IT drive should be mapped to only users who belong to IT group. Then it should map User drives some of our users have things saved in the server not all users have so those drives should be mapped according to the user name E.g. If user name is “jsmith” then map \\servername\User\jsmith. Then what I want to do is map printers this should be done according to the which group they belong E.g if user belongs to IT group then map \\servername\Printer1,

I appreciate your response.
Thank you
Dheenu
0
Comment
Question by:nbcintern
[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
  • 4
  • 2
6 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 12317079

What scripting language are you using?

Both VBScript and Kixtart Script will work, just depends on which you prefer.

%UserName% is a Windows Environmental Variable, that should work quite nicely for mapping the users folders.

Anyway, have a pseudo-code version

' Global Drive Mappings

Map P: \\Server\Contact List
Map H: \\Server\Share\%UserName%

' Group Based Drive Mappings

If User In Group IT
    Map R: \\Server\IT Share
    Install Printer to \\Share\IT Printer
End If Statement

0
 

Author Comment

by:nbcintern
ID: 12317327
I am familiar with VB and Java script Either VB or Java script is fine with me. “pseudo-code” version that you have provide can that be used same as VB or java script what’s the extension that I should save this “pseudo-code” file,
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 12317583

Pseudo-code is just fake code, a sort of design process so you know what you want to do before obscuring it with lots of real code.

If you prefer VB then the script can be written in VB... Hopefully the following will provide you with enough of a beginning to continue.

To map a drive is easy:

' Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")

' Global Drive Mappings

objNetwork.MapNetworkDrive "N:", "\\server\share"

But with that script we have no error control and it'll drop out of the script if the drive is already mapped.

So now a version with a bit of error control:

' Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Global Drive Mappings

strDrive = "N:"
strSharePath = "\\server\share"

bolDrive = objFileSystem.DriveExists(strDrive)

If (bolDrive = FALSE) Then
    objNetwork.MapNetworkDrive strDrive, strSharePath
Else
    objNetwork.RemoveNetworkDrive strDrive
    objNetwork.MapNetworkDrive strDrive, strSharePath
End If

For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

  Set objNetwork = CreateObject("WScript.Network")
  strDomain = objNetwork.UserDomain
  strUser = objNetwork.UserName
  bolIsMember = FALSE
  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
  For Each objGroup In objUser.Groups
    If objGroup.Name = strGroupName Then
      bolIsMember = TRUE
      Exit For
    End If
  Next
  IsMember = bolIsMember
  Set objUser = nothing
  Set objNetwork = nothing

End Function

Finally... to map Printers you can use:

Dim objNetwork
Dim strNetworkPath

Set objNetwork = CreateObject("WScript.Network")

strNetworkPath = "\\Server\Printer"
objNetwork.AddWindowsPrinterConnection strNetworkPath

All that remains is to glue it all together.

Hope that lot helps.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:nbcintern
ID: 12326599
Ok let see for example if I use the a script like the following one

‘Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Global Drive Mappings

strDrive = "T:"
strSharePath = "\\server01\Telephone List"

bolDrive = objFileSystem.DriveExists(strDrive)

If (bolDrive = FALSE) Then
    objNetwork.MapNetworkDrive strDrive, strSharePath
Else
    objNetwork.RemoveNetworkDrive strDrive
    objNetwork.MapNetworkDrive strDrive, strSharePath
End If

'For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

  Set objNetwork = CreateObject("WScript.Network")
  strDomain = objNetwork.UserDomain
  strUser = objNetwork.UserName
  bolIsMember = FALSE
  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
  For Each objGroup In objUser.Groups
    If objGroup.Name = strGroupName Then
      bolIsMember = TRUE
      Exit For
    End If
  Next
  IsMember = bolIsMember
  Set objUser = nothing
  Set objNetwork = nothing

End Function


So how will make this map the users personal drive I think the above script is mapping all the common drives. How can I expand it to map specific user drives and I want to printers to be mapped according to user groups too for Example IT group should get some printers and some other group should get some other set of printers

Thank you
Dheenu
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 12328210

All the function does it return True or False depending on whether the user is in a Group or not. We can use that in an If Statement to perform a drive mapping for a particular group.

‘Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Map if user is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
End If

' Map a Printer if the User is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' Or all as one statement:

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

  Set objNetwork = CreateObject("WScript.Network")
  strDomain = objNetwork.UserDomain
  strUser = objNetwork.UserName
  bolIsMember = FALSE
  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
  For Each objGroup In objUser.Groups
    If objGroup.Name = strGroupName Then
      bolIsMember = TRUE
      Exit For
    End If
  Next
  IsMember = bolIsMember
  Set objUser = nothing
  Set objNetwork = nothing

End Function
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 125 total points
ID: 12328232

One edit for the above (typos...)...

‘Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath, strNetworkPath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Map if user is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
End If

' Map a Printer if the User is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' Or all as one statement:

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' Close everything down

Set objNetwork = nothing
Set objFileSystem = nothing

' For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

    Dim strDomain, strUser, strGroupName
    Dim objUser, objGroup
    Dim bolIsMember

    strDomain = objNetwork.UserDomain
    strUser = objNetwork.UserName
    bolIsMember = FALSE
    Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
    For Each objGroup In objUser.Groups
        If objGroup.Name = strGroupName Then
            bolIsMember = TRUE
            Exit For
        End If
    Next
    IsMember = bolIsMember

    Set objUser = nothing

End Function
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

626 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