Advertisement
Calvus

VBScript - Install printers

Apr 15th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '===========================================
  2. 'Dynamic Printer Deployment based on AD Group Membership
  3. 'Author: Robert Gransbury
  4. 'Source: Microsoft Technet (http://gallery.technet.microsoft.com/scriptcenter/bf160908-93e3-484c-944f-1c95004c5498)
  5. 'Modified By: Jason Coleman, 3/27/2012
  6. 'This script adds printers upon user login based on their AD Group membership.
  7. 'It looks for groups in the "Printer Paths" OU and attempts to install the printers in those groups' descriptions.  
  8. 'It also looks for groups in the "Printer Groups" OU and installs printers based on what "Printer Paths" groups those groups are in.
  9. 'It assigns default printers by membership in the "Printer Defaults" OU groups.  If an account is a member of more than one "Printer Defaults" group, unexpected results may occur.
  10. '===========================================
  11.  
  12. on error resume next
  13. Dim objADSystemInfo, objUser, objMemberOf, objChildMemberOf, objGroup, objChildGroup, objGroupEnum, objNetwork, objShell, objPrinter
  14. Dim i, bTroubleFlag
  15.  
  16. Set objShell = CreateObject("WScript.Shell")
  17. Set objNetwork = CreateObject("Wscript.Network")
  18.  
  19. WScript.sleep 25000
  20.  
  21. 'Get current user info from active directory
  22. Set objADSystemInfo = CreateObject("ADSystemInfo")
  23. 'bind to current user in active directory
  24. Set objUser = GetObject("LDAP://" & objADSystemInfo.UserName)
  25. 'Tests to see if verbose messages should be displayed or not by looking at the user's description
  26. if objuser.description = "printer.trouble" then
  27.     bTroubleFlag = true
  28.     msgbox "Troubleshooting Printer Logon Script"
  29. end if
  30.  
  31. 'Deletes any currently installed network Printers
  32. Set objPrinter = objNetwork.EnumPrinterConnections
  33. 'Test to see if we have any printers mapped
  34. If objPrinter.Count > 0 Then
  35.     'The Printer array is Printer name, printer path that is why it is step 2
  36.    for i=1 to objPrinter.Count Step 2
  37.         'test to make sure it is a network printer
  38.        if instr(objPrinter.Item(i),"\\") <> 0 then
  39.             if bTroubleFlag then
  40.                 msgbox "Deleting:" & vbcrlf & objPrinter.Item(i)
  41.             end if
  42.             objNetwork.RemovePrinterConnection objPrinter.Item(i),true,true
  43.         end if
  44.     next
  45. end if
  46.  
  47. 'Get an array of group names that the user is a member of
  48. objMemberOf = objUser.MemberOf
  49. 'Adds appropriate printers based on the user's group membership and sets default printer
  50. for Each objGroup in objMemberOf
  51.     'Test to see if it is a printer group. all printer groups should be in this OU.
  52.                if (instr(objGroup,"OU=Printer Groups") <> 0) then
  53.                                 'If the group is in the Printer Groups OU, find that Group's parents and pass them to AddPrinter function
  54.        if bTroubleFlag then
  55.             msgbox "Found Printer Group: " & objGroup
  56.         end if
  57.                                 objChildMemberOf = (GetObject("LDAP://" & objGroup)).MemberOf
  58.                                 for Each objChildGroup in objChildMemberOf
  59.                                                 if (objChildGroup = "") then
  60.                                                                 'if it is blank, it's because objChildMemberOf is a string because there was only 1 group that objGroup is a member of and so it did not return an array as expected
  61.                                                                AddPrinter objChildMemberOf
  62.                                                 else
  63.                                                                 AddPrinter objChildGroup
  64.                                                 end if
  65.                                 next
  66.                 else
  67.                                 'If the group is not in the Printer Groups OU, pass it to AddPrinter function for analysis
  68.        if bTroubleFlag then
  69.             msgbox "Found non Printer Group: " & objGroup
  70.         end if
  71.                                 AddPrinter objGroup
  72.                 end if
  73. next
  74.  
  75. 'Sets default printer based on group membership within the Printer Defaults OU
  76. for Each objGroup in objMemberOf
  77.     if (instr(objGroup,"OU=Printer Defaults") <> 0) then
  78.         set objGroupEnum = GetObject("LDAP://" & objGroup)
  79.         if bTroubleFlag then
  80.             msgbox "Setting Default:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description
  81.         end if
  82.         objNetwork.SetDefaultPrinter objGroupEnum.description
  83.         set objGroupEnum = nothing
  84.     end if
  85. next
  86.  
  87. if bTroubleFlag then
  88.     msgbox "Printer Logon Script Finished"
  89. end if
  90.  
  91. Function AddPrinter(objIn)
  92. 'Function expects to be passed an LDAP string for a group
  93. 'Validates that an object is in the correct OU and attempt to find a UNC path in the description to install the printer.
  94. if (instr(objIn,"OU=Printer Paths") <> 0) then
  95.                 'Bind to the group to get is description. The description contain the path to the printer
  96.                set objGroupEnum = GetObject("LDAP://" & objIn)
  97.                 if bTroubleFlag then
  98.                                 msgbox "Adding:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description
  99.                 end if
  100.                 objNetwork.AddWindowsPrinterConnection objGroupEnum.description
  101.                 set objGroupEnum = nothing
  102. end if
  103. end Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement