Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Log on scripts Windows 2003 Server

Posted on 2004-10-14
6
Medium Priority
?
272 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 500 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

704 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