1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Logon Scripts, lets share and improve

Discussion in 'Scripting & Programming' started by SimonV, Aug 11, 2006.

  1. SimonV

    SimonV Petabyte Poster Administrator

    6,616
    149
    228
    I'm just about to start work on a VBS logon script that will map printers and shares for my users on our network, I'm looking to install printers on computers based on the active directory OU that they are located and also by user group membership of my users. Im also looking to map drives based on user group membership.

    So I wondered before I started this how others accomplished this and the methods they used, maybe we could have a recourse sharing thread and post our scripts and that way we may help others working on their own scripts.

    I'll post mine later when I have got it ironed out and tested.

    Dont forget to use the code tags when posting your code in the forums

    this:
    [CODE]code goes here[/CODE]

    will look like this:
    Code:
    code goes here
     
    Certifications: MOS Master 2003, CompTIA A+, MCSA:M, MCSE
    WIP: Keeping CF Alive...
  2. Boycie
    Honorary Member

    Boycie Senior Beer Tester

    6,281
    85
    174
    Something i know nothing about, so look forward to any posts on the matter.

    Si
     
    Certifications: MCSA 2003, MCDST, A+, N+, CTT+, MCT
  3. SimonV

    SimonV Petabyte Poster Administrator

    6,616
    149
    228
    Here is my first part, this will map 2 drives G: and H: for every user and four additional drives for two differnet usergroups "technicians" and "admin staff", now I'll add all other groups and the drives that they need to the script follwing the same format. I wont post the full script just parts of it. HTH


    Code:
    '--------Mapping drives and printer script --------
    '--------by SimonV www.certforums.co.uk --------
    Option Explicit
    On Error Resume Next
    
    Dim DriveLetter1, DriveLetter2, DriveLetter3, DriveLetter4
    Dim DriveLetter5, DriveLetter6
    Dim RemotePath1, RemotePath2, RemotePath3, RemotePath4
    Dim RemotePath5, RemotePath6
    Dim objNetwork, objUser, CurrentUser
    Dim strGroup
    Dim strList
    Dim objSysinfo
    
    Const Technicians_Group = "cn=technicians"
    Const AdminStaff_Group = "cn=admin staff"
    
    Set objNetwork = CreateObject("WScript.Network")
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
    strGroup = LCase(Join(CurrentUser.MemberOf))
    strList = objSysinfo.Computername
    
    
    DriveLetter1 = "G:"
    DriveLetter2 = "H:"
    RemotePath1 = "\\admin-server1\dida"
    RemotePath2 = "\\net-server1\coursework"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    
    '-------- Map Drives by Group Membership --------
    
    '*******Technicians_Group*******
    If InStr(strGroup, Technicians_Group ) Then
    DriveLetter3 = "M:"
    DriveLetter4 = "N:"
    DriveLetter5 = "S:"
    DriveLetter6 = "Z:"
    RemotePath3 = "\\NET-SERVER3\miscfiles$"
    RemotePath4 = "\\admin-server2\common"
    RemotePath5 = "\\NET-SERVER2\Software"
    RemotePath6 = "\\net-server3\administration$"
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    objNetwork.MapNetworkDrive DriveLetter4, RemotePath4
    objNetwork.MapNetworkDrive DriveLetter5, RemotePath5
    objNetwork.MapNetworkDrive DriveLetter6, RemotePath6
    End If
    
    '*******AdminStaff_Group********
    If InStr(strGroup, AdminStaff_Group ) Then
    DriveLetter3 = "P:"
    DriveLetter4 = "M:"
    DriveLetter5 = "S:"
    DriveLetter6 = "N:"
    RemotePath3 = "\\admin-server2\fileroom"
    RemotePath4 = "\\admin-server2\clerks"
    RemotePath5 = "\\admin-server2\stnddocs"
    RemotePath6 = "\\admin-server2\common"
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    objNetwork.MapNetworkDrive DriveLetter4, RemotePath4
    objNetwork.MapNetworkDrive DriveLetter5, RemotePath5
    objNetwork.MapNetworkDrive DriveLetter6, RemotePath6
    End If
    
    WScript.Quit 
    
     
    
     
     
    Last edited: Jan 2, 2015
    Certifications: MOS Master 2003, CompTIA A+, MCSA:M, MCSE
    WIP: Keeping CF Alive...
  4. SimonV

    SimonV Petabyte Poster Administrator

    6,616
    149
    228
    Well I carried on and have nearly completed my script and here is a copy, maybe we could do some scripting threads to explain the whole process and all put in ideas.

    Code:
    Option Explicit
    on error resume next
    
    Dim DriveLetter1, DriveLetter2, DriveLetter3, DriveLetter4
    Dim DriveLetter5, DriveLetter6, DriveLetter7, DriveLetter8
    Dim DriveLetter9
    Dim RemotePath1, RemotePath2, RemotePath3, RemotePath4
    Dim RemotePath5, RemotePath6, RemotePath7, RemotePath8
    Dim RemotePath9
    Dim objNetwork, objUser, CurrentUser
    Dim strGroup
    Dim strList
    Dim objSysinfo
    Dim objPrinter
    
    Const Staff_Group = "cn=staff"
    Const Support_Group = "cn=support staff"
    Const Technicians_Group = "cn=technicians"
    Const Pupils_Group = "cn=pupils group"
    Const Behavmon_Group = "cn=behavmon users"
    Const AdminStaff_Group = "cn=admin staff"
    Const DSAS_Group = "cn=dsas users"
    Const Facilty_Group = "cn=facility users"
    Const FileRoomUsers_Group = "cn=file room users"
    Const HeadsOfYear_Group = "cn=heads of year"
    Const IntegrisExportUsers_Group = "cn=integris export users"
    
    Set objNetwork = CreateObject("WScript.Network")
    Set objPrinter = CreateObject("WScript.Network")
    Set objSysinfo = CreateObject("ADSystemInfo")
    
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
    strGroup = LCase(Join(CurrentUser.MemberOf))
    strList = objSysinfo.Computername
    
    '-------- Map Drives by Group Membership --------
    
    '*******Technicians_Group*******
    If InStr(strGroup, Technicians_Group ) Then
    DriveLetter1 = "F:"
    DriveLetter2 = "G:"
    DriveLetter3 = "H:"
    DriveLetter4 = "I:"
    DriveLetter5 = "K:"
    DriveLetter6 = "M:"
    DriveLetter7 = "N:"
    DriveLetter8 = "S:"
    DriveLetter9 = "Z:"
    RemotePath1 = "\\admin-server2\fileroom"
    RemotePath2 = "\\admin-server2\clerks"
    RemotePath3 = "\\admin-server1\finance"
    RemotePath4 = "\\server1\bromcom"
    RemotePath5 = "\\admin-server2\behavmon"
    RemotePath6 = "\\NET-SERVER3\miscfiles$"
    RemotePath7 = "\\admin-server2\common"
    RemotePath8 = "\\admin-server2\stnddocs"
    RemotePath9 = "\\net-server3\administration$"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    objNetwork.MapNetworkDrive DriveLetter4, RemotePath4
    objNetwork.MapNetworkDrive DriveLetter5, RemotePath5
    objNetwork.MapNetworkDrive DriveLetter6, RemotePath6
    objNetwork.MapNetworkDrive DriveLetter7, RemotePath7
    objNetwork.MapNetworkDrive DriveLetter8, RemotePath8
    objNetwork.MapNetworkDrive DriveLetter9, RemotePath9
    End If
    
    '*******AdminStaff_Group********
    If InStr(strGroup, AdminStaff_Group ) Then
    DriveLetter1 = "F:"
    DriveLetter2 = "G:"
    DriveLetter3 = "S:"
    DriveLetter4 = "N:"
    RemotePath1 = "\\admin-server2\fileroom"
    RemotePath2 = "\\admin-server2\clerks"
    RemotePath3 = "\\admin-server2\stnddocs"
    RemotePath4 = "\\admin-server2\common"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    objNetwork.MapNetworkDrive DriveLetter4, RemotePath4
    End If
    
    '*******Staff_Group*******
    If InStr(strGroup, Staff_Group ) Then
    DriveLetter1 = "M:"
    DriveLetter2 = "N:"
    DriveLetter3 = "G:"
    RemotePath1 = "\\NET-SERVER3\miscfiles$"
    RemotePath2 = "\\admin-server2\common"
    RemotePath3 = "\\net-server1\global"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    End If
    
    '*******FileRoomUsers_Group*******
    If InStr(strGroup, FileRoomUsers_Group ) Then
    DriveLetter1 = "F:"
    RemotePath1 = "\\admin-server2\fileroom"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    End If
    
    '*******Behavmon_Group*******
    If InStr(strGroup, Behavmon_Group ) Then
    DriveLetter1 = "K:"
    RemotePath1 = "\\admin-server2\behavmon"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    End If
    
    '*******HeadsOfYear_Group*******
    If InStr(strGroup, HeadsOfYear_Group ) Then
    DriveLetter1 = "K:"
    RemotePath1 = "\\admin-server2\behavmon"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    End If
    
    '*******DSAS_Group*******
    If InStr(strGroup, DSAS_Group ) Then
    DriveLetter1 = "H:"
    RemotePath1 = "\\admin-server1\finance"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    End If
    
    '*******IntegrisExportUsers_Group*******
    If InStr(strGroup, IntegrisExportUsers_Group ) Then
    DriveLetter1 = "I:"
    RemotePath1 = "\\server1\bromcom"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    End If
    
    '*******Pupils_Group*******
    If InStr(strGroup, Pupils_Group ) Then
    DriveLetter1 = "M:"
    DriveLetter2 = "G:"
    RemotePath1 = "\\NET-SERVER3\miscfiles$"
    RemotePath2 = "\\net-server1\global"
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    End If
    
    '-------- Assign Printer by Group Membership --------
    If InStr(strGroup, Technicians_Group ) Then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3d-hp2230"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3c-hp2200"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\T5A-HP1100"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\library-hp1100"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\C2G-HP2230"
    End If
    
    '-------- Assign Printer by OU's --------
    
    If instr(strList, "OU=C3D") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3d-hp2230"
    objPrinter.SetDefaultPrinter "\\admin-server2\c3d-hp2230"
    
    ElseIf instr(strList, "OU=C3C") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3c-hp2200"
    objPrinter.SetDefaultPrinter "\\admin-server2\c3c-hp2200"
    
    ElseIf instr(strList, "OU=T5A") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\T5A-HP1100"
    objPrinter.SetDefaultPrinter "\\admin-server2\T5A-HP1100"
    
    ElseIf instr(strList, "OU=C2G") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\C2G-HP2230"
    objPrinter.SetDefaultPrinter "\\admin-server2\C2G-HP2230"
    
    ElseIf instr(strList, "OU=Library") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\library-hp1100"
    objPrinter.SetDefaultPrinter "\\admin-server2\library-hp1100"
    
    End If
    
    WScript.Quit 
    
     
    Certifications: MOS Master 2003, CompTIA A+, MCSA:M, MCSE
    WIP: Keeping CF Alive...
  5. Boycie
    Honorary Member

    Boycie Senior Beer Tester

    6,281
    85
    174
    yes please. :biggrin
     
    Last edited by a moderator: Jan 2, 2015
    Certifications: MCSA 2003, MCDST, A+, N+, CTT+, MCT
  6. jackd

    jackd Megabyte Poster

    555
    7
    64
    Yer Cool! :biggrin
     
  7. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    That's a pretty handy script, SimonV. I have a quick question though. Do you not comment your scripts or, do you just comment your complex scripts?

    I ask because I've found that commenting my bash scripts makes it a lot easier to not only read them but to remember exactly why I did what I did a few months later.
     
    Last edited by a moderator: Jan 2, 2015
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  8. Sparky
    Highly Decorated Member Award

    Sparky Zettabyte Poster Moderator

    10,190
    296
    319
    Impressive script, looks like it will save time messing around with batch files.

    Have you tested it? I tried to map drives and add printers with vbs but when the user logged on for the second it would throw a ‘drive letter in use’ error but I think the ‘On error’ part of the code you have added might get you around that problem. 8)
     
    Certifications: MSc MCSE MCSA:M MCSA:S MCITP:EA MCTS(x5) Security+ Network+ A+
    WIP: Exchange 2007\2010
  9. SimonV

    SimonV Petabyte Poster Administrator

    6,616
    149
    228
    After some testing and fine tuning my logon script is about complete, heres a copy:

    NOTE: The previous scripts will not work with users that are members of one usergroup (not including domain users), there is a workaround in this script that will rectify that.
    Code:
    '--------Mapping drives and printer script --------
    '--------by SimonV www.certforums.co.uk --------
    Option Explicit
    on error resume next
    
    '--------Declares variables and allocates storage space with Dim------
    Dim DriveLetter1, DriveLetter2, DriveLetter3, DriveLetter4
    Dim DriveLetter5, DriveLetter6, DriveLetter7, DriveLetter8
    Dim DriveLetter9, DriveLetter10
    Dim RemotePath1, RemotePath2, RemotePath3, RemotePath4
    Dim RemotePath5, RemotePath6, RemotePath7, RemotePath8
    Dim RemotePath9, RemotePath10
    Dim objNetwork, objUser, CurrentUser
    Dim strGroup
    Dim strList
    Dim objSysinfo
    Dim objPrinter
    
    '-------- Initialise Groups with Const (cn names must be lowercase)  --------
    Const Technicians_Group = "cn=technicians"
    Const AdminStaff_Group = "cn=admin staff"
    Const Teachers_Group = "cn=teachers"
    Const Support_Group = "cn=support staff"
    Const StudentTeachers_Group = "cn=student teachers"
    Const Pupils_Group = "cn=pupils group"
    Const FileRoomUsers_Group = "cn=file room users"
    Const Behavmon_Group = "cn=behavmon users"
    Const HeadsOfYear_Group = "cn=heads of year"
    Const DSAS_Group = "cn=dsas users"
    Const IntegrisExportUsers_Group = "cn=integris export users"
    
    '-------- Create objects and extract values --------
    Set objNetwork = CreateObject("WScript.Network")
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
    Set objPrinter = CreateObject("WScript.Network")
    Set objSysinfo = CreateObject("ADSystemInfo")
    strList = objSysinfo.Computername
    
    '-------- workaround for Join not supporting single group membership --------
    If IsArray(CurrentUser.MemberOf) Then
    strGroup = LCase(Join(CurrentUser.MemberOf))
    Else
    strGroup = lcase(CurrentUser.MemberOf) 
    End If
    
    
    
    '-------- Contruct drive mapping --------
    DriveLetter1 = "F:"
    DriveLetter2 = "G:"
    DriveLetter3 = "H:"
    DriveLetter4 = "I:"
    DriveLetter5 = "K:"
    DriveLetter6 = "L:"
    DriveLetter7 = "M:"
    DriveLetter8 = "N:"
    DriveLetter9 = "S:"
    DriveLetter10 = "Z:"
    RemotePath1 = "\\admin-server2\fileroom"
    RemotePath2 = "\\net-server1\global"
    RemotePath3 = "\\admin-server1\finance"
    RemotePath4 = "\\server1\bromcom"
    RemotePath5 = "\\admin-server2\behavmon"
    RemotePath6 = "\\admin-server2\clerks"
    RemotePath7 = "\\NET-SERVER3\miscfiles$"
    RemotePath8 = "\\admin-server2\common"
    RemotePath9 = "\\admin-server2\stnddocs"
    RemotePath10 = "\\net-server3\administration$"
    
    '--------START Map Drives by Group Membership --------
    '*******Technicians_Group*******
    If InStr(strGroup, Technicians_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    objNetwork.MapNetworkDrive DriveLetter4, RemotePath4
    objNetwork.MapNetworkDrive DriveLetter5, RemotePath5
    objNetwork.MapNetworkDrive DriveLetter6, RemotePath6
    objNetwork.MapNetworkDrive DriveLetter7, RemotePath7
    objNetwork.MapNetworkDrive DriveLetter8, RemotePath8
    objNetwork.MapNetworkDrive DriveLetter9, RemotePath9
    objNetwork.MapNetworkDrive DriveLetter10, RemotePath10
    End If
    
    '*******AdminStaff_Group********
    If InStr(strGroup, AdminStaff_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    objNetwork.MapNetworkDrive DriveLetter6, RemotePath6
    objNetwork.MapNetworkDrive DriveLetter8, RemotePath8
    objNetwork.MapNetworkDrive DriveLetter9, RemotePath9
    End If
    
    '*******Teachers_Group*******
    If InStr(strGroup, Teachers_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter7, RemotePath7
    objNetwork.MapNetworkDrive DriveLetter8, RemotePath8
    End If
    
    '*******Support_Group*******
    If InStr(strGroup, Support_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter7, RemotePath7
    objNetwork.MapNetworkDrive DriveLetter8, RemotePath8
    End If
    
    '*******StudentTeachers_Group*******
    If InStr(strGroup, StudentTeachers_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter2, RemotePath2
    objNetwork.MapNetworkDrive DriveLetter7, RemotePath7
    objNetwork.MapNetworkDrive DriveLetter8, RemotePath8
    End If
    
    '*******Pupils_Group*******
    If InStr(strGroup, Pupils_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter7, RemotePath7
    End If
    
    '*******FileRoomUsers_Group*******
    If InStr(strGroup, FileRoomUsers_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
    End If
    
    '*******Behavmon_Group*******
    If InStr(strGroup, Behavmon_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter5, RemotePath5
    End If
    
    '*******HeadsOfYear_Group*******
    If InStr(strGroup, HeadsOfYear_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter5, RemotePath5
    End If
    
    '*******DSAS_Group*******
    If InStr(strGroup, DSAS_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter3, RemotePath3
    End If
    
    '*******IntegrisExportUsers_Group*******
    If InStr(strGroup, IntegrisExportUsers_Group ) Then
    objNetwork.MapNetworkDrive DriveLetter4, RemotePath4
    End If
    
    '--------END Map Drives by Group Membership --------
    
    
    '-------- Assign Printer by Group Membership --------
    If InStr(strGroup, Technicians_Group ) Then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3d-hp2230"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3c-hp2200"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\T5A-HP1100"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\library-hp1100"
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\C2G-HP2230"
    End If
    
    '-------- Assign Printer by OU's --------
    
    If instr(strList, "OU=C3D") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3d-hp2230"
    objPrinter.SetDefaultPrinter "\\admin-server2\c3d-hp2230"
    
    ElseIf instr(strList, "OU=C3C") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\c3c-hp2200"
    objPrinter.SetDefaultPrinter "\\admin-server2\c3c-hp2200"
    
    ElseIf instr(strList, "OU=T5A") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\T5A-HP1100"
    objPrinter.SetDefaultPrinter "\\admin-server2\T5A-HP1100"
    
    ElseIf instr(strList, "OU=C2G") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\C2G-HP2230"
    objPrinter.SetDefaultPrinter "\\admin-server2\C2G-HP2230"
    
    ElseIf instr(strList, "OU=Library") then
    objPrinter.AddWindowsPrinterConnection "\\admin-server2\library-hp1100"
    objPrinter.SetDefaultPrinter "\\admin-server2\library-hp1100"
    
    
    End If
    
    WScript.Quit 
    
     
    Last edited: Jan 2, 2015
    Certifications: MOS Master 2003, CompTIA A+, MCSA:M, MCSE
    WIP: Keeping CF Alive...
  10. SimonV

    SimonV Petabyte Poster Administrator

    6,616
    149
    228
    Has anyone got any use from this script BTW, I'll try and do a walkthrough at some point but my time is limited so it may take a while. :)

    Si
     
    Certifications: MOS Master 2003, CompTIA A+, MCSA:M, MCSE
    WIP: Keeping CF Alive...
  11. Luddym

    Luddym Megabyte Poster

    797
    19
    74
    Sorry for adding to a thread that is quite old, but.... I'd just like to thank you SimonV for sharing the script. I've been looking about today for solutions to my problems, and this looks like just the ticket.

    I'm actually looking forward to work next week so I can edit and implement this, so we can finally get rid of all those rubbish logon scripts we have at present.

    Thanks again, and rep quite rightly given.
     
    Last edited by a moderator: Jan 2, 2015
    Certifications: VCP,A+, N+, MCSA, MCSE
    WIP: Christmas Drunkard

Share This Page

Loading...