' Logon3.vbs ' VBScript logon script program. ' ' ---------------------------------------------------------------------- ' Copyright (c) 2002 Richard L. Mueller ' Hilltop Lab web site - http://www.rlmueller.net ' Version 1.0 - November 22, 2002 ' Version 1.1 - February 19, 2003 - Standardize Hungarian notation. ' Version 1.2 - January 25, 2004 - Modify error trapping. ' Version 1.3 - July 31, 2007 - Escape any "/" characters in DN's. ' ' You have a royalty-free right to use, modify, reproduce, and ' distribute this script file in any way you find useful, provided that ' you agree that the copyright owner above has no warranty, obligations, ' or liability for such use. Option Explicit 'On Error Resume Next Dim objNetwork, objSysInfo, strUserDN Dim objGroupList, objUser, objFSO Dim strComputerDN, objComputer Dim objDrive, objShell, objShare, objShareName Dim WSHPrinters, LOOP_COUNTER Set objNetwork = CreateObject("Wscript.Network") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objSysInfo = CreateObject("ADSystemInfo") Set objShell = CreateObject("Shell.Application") strUserDN = objSysInfo.userName strComputerDN = objSysInfo.computerName LOOP_COUNTER = 0 Set WSHPrinters = objNetwork.EnumPrinterConnections ' Escape any forward slash characters, "/", with the backslash ' escape character. All other characters that should be escaped are. strUserDN = Replace(strUserDN, "/", "\/") strComputerDN = Replace(strComputerDN, "/", "\/") ' Bind to the user and computer objects with the LDAP provider. Set objUser = GetObject("LDAP://" & strUserDN) Set objComputer = GetObject("LDAP://" & strComputerDN) 'Remove ALL network printers 'Enumerate all printers first, after that you can select the printers you want by performing some string checks For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 'To remove only networked printers use this If Statement If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then objNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True End If 'To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above 'WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True Next 'To remove a specific printer...use code like line below 'objNetwork.RemovePrinterConnection "\\ServerOld\HP5si",True,True 'Define Printer Names Dim HsMfu, MsMfu, EsMfu, SuptMfu, TransMfu, HsOfficeLaser, EsOfficeLaser Dim EsLabLaser, MsLabLaser, HsLabLaser Dim YearbookLaser, MobileLabLaser, E2020LabLaser, OpenLabLaser HsMfu = "\\stuprint1\hsmfu" MsMfu = "\\stuprint1\msmfu" EsMfu = "\\stuprint1\esmfu" SuptMfu = "\\stuprint1\suptmfu" TransMfu = "\\stuprint1\transmfu" HsOfficeLaser = "\\stuprint1\hsofficelaser" EsOfficeLaser = "\\stuprint1\EsOfficeLaser" EsLabLaser = "\\stuprint1\EsLabLaser" MsLabLaser = "\\stuprint1\MsLabLaser" HsLabLaser = "\\stuprint1\HsLabLaser" YearbookLaser = "\\stuprint1\yearbooklaser" MobileLabLaser = "\\stuprint1\mobilelablaser" E2020LabLaser = "\\stuprint1\e2020lablaser" OpenLabLaser = "\\stuprint1\openlablaser" ' Add a network printer if the computer is a member of the group. If (IsMember(objUser, "Elementary School Staff Printers") = True) Then objNetwork.AddWindowsPrinterConnection EsMfu objNetwork.AddWindowsPrinterConnection MsMfu objNetwork.AddWindowsPrinterConnection HsMfu objNetwork.AddWindowsPrinterConnection EsLabLaser objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.SetDefaultPrinter EsMfu End If If (IsMember(objUser, "Elementary School Student Printers") = True) Then objNetwork.AddWindowsPrinterConnection EsMfu objNetwork.AddWindowsPrinterConnection EsLabLaser objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.SetDefaultPrinter EsLabLaser End If If (IsMember(objUser, "Middle School Staff Printers") = True) Then objNetwork.AddWindowsPrinterConnection EsMfu objNetwork.AddWindowsPrinterConnection MsMfu objNetwork.AddWindowsPrinterConnection HsMfu objNetwork.AddWindowsPrinterConnection MsLabLaser objNetwork.AddWindowsPrinterConnection OpenLabLaser objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.SetDefaultPrinter MsMfu End If If (IsMember(objUser, "Middle School Student Printers") = True) Then objNetwork.AddWindowsPrinterConnection MsMfu objNetwork.AddWindowsPrinterConnection MsLabLaser objNetwork.AddWindowsPrinterConnection OpenLabLaser objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.SetDefaultPrinter MsLabLaser End If If (IsMember(objUser, "High School Staff Printers") = True) Then objNetwork.AddWindowsPrinterConnection EsMfu objNetwork.AddWindowsPrinterConnection MsMfu objNetwork.AddWindowsPrinterConnection HsMfu objNetwork.AddWindowsPrinterConnection HsLabLaser objNetwork.AddWindowsPrinterConnection OpenLabLaser objNetwork.AddWindowsPrinterConnection E2020LabLaser objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.SetDefaultPrinter HsMfu End If If (IsMember(objUser, "High School Student Printers") = True) Then objNetwork.AddWindowsPrinterConnection HsMfu objNetwork.AddWindowsPrinterConnection HsLabLaser objNetwork.AddWindowsPrinterConnection E2020LabLaser objNetwork.AddWindowsPrinterConnection OpenLabLaser objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.SetDefaultPrinter HsLabLaser End If If (IsMember(objUser, "Yearbook Printers") = True) Then objNetwork.AddWindowsPrinterConnection YearbookLaser End If If (IsMember(objUser, "E2020 Printers") = True) Then objNetwork.SetDefaultPrinter E2020LabLaser End If If (IsMember(objUser, "Transportation Printers") = True) Then objNetwork.AddWindowsPrinterConnection TransMfu objNetwork.SetDefaultPrinter TransMfu End If If (IsMember(objUser, "HS Office Printers") = True) Then objNetwork.AddWindowsPrinterConnection HsOfficeLaser objNetwork.SetDefaultPrinter HsOfficeLaser End If If (IsMember(objUser, "ES Office Printers") = True) Then objNetwork.AddWindowsPrinterConnection EsOfficeLaser objNetwork.SetDefaultPrinter EsOfficeLaser End If If (IsMember(objUser, "Technology Staff") = True) Then objNetwork.AddWindowsPrinterConnection EsMfu objNetwork.AddWindowsPrinterConnection MsMfu objNetwork.AddWindowsPrinterConnection HsMfu objNetwork.AddWindowsPrinterConnection MobileLabLaser objNetwork.AddWindowsPrinterConnection HsLabLaser objNetwork.AddWindowsPrinterConnection MsLabLaser objNetwork.AddWindowsPrinterConnection EsLabLaser objNetwork.AddWindowsPrinterConnection TransMfu objNetwork.AddWindowsPrinterConnection SuptMfu objNetwork.AddWindowsPrinterConnection HsOfficeLaser objNetwork.AddWindowsPrinterConnection YearBookLaser objNetwork.AddWindowsPrinterConnection E2020LabLaser objNetwork.AddWindowsPrinterConnection OpenLabLaser End If 'Set local printer to default (if possible) 'A or a = would equal Adobe (don't want that) 'M or m = microsoft print to file (don't want that either) 'will catch any hp, xerox (or workcentre), epson stuff 'anything else - just put an underscore in front of the printer! For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2 'Find local printers If Left(WSHPrinters.Item(LOOP_COUNTER +1),1) = "H" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "h" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "E" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "e" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "X" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "x" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "W" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "w" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) ElseIf Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "_" Then objNetwork.SetDefaultPrinter(WSHPrinters.Item(LOOP_COUNTER +1)) End If Next ' Clean up. Set objNetwork = Nothing Set objFSO = Nothing Set objSysInfo = Nothing Set objGroupList = Nothing Set objUser = Nothing Set objComputer = Nothing Set objDrive = Nothing Set objShare = Nothing Set objShell = Nothing Set WSHPrinters = Nothing Set LOOP_COUNTER = Nothing HsMfu = "" MsMfu = "" EsMfu = "" SuptMfu = "" TransMfu = "" HsOfficeLaser = "" EsOfficeLaser = "" EsLabLaser = "" MsLabLaser = "" HsLabLaser = "" YearbookLaser = "" MobileLabLaser = "" E2020LabLaser = "" OpenLabLaser = "" Function IsMember(ByVal objADObject, ByVal strGroup) ' Function to test for group membership. ' objGroupList is a dictionary object with global scope. If (IsEmpty(objGroupList) = True) Then Set objGroupList = CreateObject("Scripting.Dictionary") End If If (objGroupList.Exists(objADObject.sAMAccountName & "\") = False) Then Call LoadGroups(objADObject, objADObject) objGroupList.Add objADObject.sAMAccountName & "\", True End If IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _ & strGroup) End Function Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject) ' Recursive subroutine to populate dictionary object objGroupList. Dim colstrGroups, objGroup, j objGroupList.CompareMode = vbTextCompare colstrGroups = objADSubObject.memberOf If (IsEmpty(colstrGroups) = True) Then Exit Sub End If If (TypeName(colstrGroups) = "String") Then ' Escape any forward slash characters, "/", with the backslash ' escape character. All other characters that should be escaped are. colstrGroups = Replace(colstrGroups, "/", "\/") Set objGroup = GetObject("LDAP://" & colstrGroups) If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _ & objGroup.sAMAccountName) = False) Then objGroupList.Add objPriObject.sAMAccountName & "\" _ & objGroup.sAMAccountName, True Call LoadGroups(objPriObject, objGroup) End If Set objGroup = Nothing Exit Sub End If For j = 0 To UBound(colstrGroups) ' Escape any forward slash characters, "/", with the backslash ' escape character. All other characters that should be escaped are. colstrGroups(j) = Replace(colstrGroups(j), "/", "\/") Set objGroup = GetObject("LDAP://" & colstrGroups(j)) If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _ & objGroup.sAMAccountName) = False) Then objGroupList.Add objPriObject.sAMAccountName & "\" _ & objGroup.sAMAccountName, True Call LoadGroups(objPriObject, objGroup) End If Next Set objGroup = Nothing End Sub Function MapDrive(ByVal strDrive, ByVal strShare) ' Function to map network share to a drive letter. ' If the drive letter specified is already in use, the function ' attempts to remove the network connection. ' objFSO is the File System Object, with global scope. ' objNetwork is the Network object, with global scope. ' Returns True if drive mapped, False otherwise. Dim objDrive On Error Resume Next If (objFSO.DriveExists(strDrive) = True) Then Set objDrive = objFSO.GetDrive(strDrive) If (Err.Number <> 0) Then On Error GoTo 0 MapDrive = False Exit Function End If If (objDrive.DriveType = 3) Then objNetwork.RemoveNetworkDrive strDrive, True, True Else MapDrive = False Exit Function End If Set objDrive = Nothing End If objNetwork.MapNetworkDrive strDrive, strShare If (Err.Number = 0) Then MapDrive = True Else Err.Clear MapDrive = False End If On Error GoTo 0 End Function