Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '===========================================
- 'Dynamic Printer Deployment based on AD Group Membership
- 'Author: Robert Gransbury
- 'Source: Microsoft Technet (http://gallery.technet.microsoft.com/scriptcenter/bf160908-93e3-484c-944f-1c95004c5498)
- 'Modified By: Jason Coleman, 3/27/2012
- 'This script adds printers upon user login based on their AD Group membership.
- 'It looks for groups in the "Printer Paths" OU and attempts to install the printers in those groups' descriptions.
- 'It also looks for groups in the "Printer Groups" OU and installs printers based on what "Printer Paths" groups those groups are in.
- '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.
- '===========================================
- on error resume next
- Dim objADSystemInfo, objUser, objMemberOf, objChildMemberOf, objGroup, objChildGroup, objGroupEnum, objNetwork, objShell, objPrinter
- Dim i, bTroubleFlag
- Set objShell = CreateObject("WScript.Shell")
- Set objNetwork = CreateObject("Wscript.Network")
- WScript.sleep 25000
- 'Get current user info from active directory
- Set objADSystemInfo = CreateObject("ADSystemInfo")
- 'bind to current user in active directory
- Set objUser = GetObject("LDAP://" & objADSystemInfo.UserName)
- 'Tests to see if verbose messages should be displayed or not by looking at the user's description
- if objuser.description = "printer.trouble" then
- bTroubleFlag = true
- msgbox "Troubleshooting Printer Logon Script"
- end if
- 'Deletes any currently installed network Printers
- Set objPrinter = objNetwork.EnumPrinterConnections
- 'Test to see if we have any printers mapped
- If objPrinter.Count > 0 Then
- 'The Printer array is Printer name, printer path that is why it is step 2
- for i=1 to objPrinter.Count Step 2
- 'test to make sure it is a network printer
- if instr(objPrinter.Item(i),"\\") <> 0 then
- if bTroubleFlag then
- msgbox "Deleting:" & vbcrlf & objPrinter.Item(i)
- end if
- objNetwork.RemovePrinterConnection objPrinter.Item(i),true,true
- end if
- next
- end if
- 'Get an array of group names that the user is a member of
- objMemberOf = objUser.MemberOf
- 'Adds appropriate printers based on the user's group membership and sets default printer
- for Each objGroup in objMemberOf
- 'Test to see if it is a printer group. all printer groups should be in this OU.
- if (instr(objGroup,"OU=Printer Groups") <> 0) then
- 'If the group is in the Printer Groups OU, find that Group's parents and pass them to AddPrinter function
- if bTroubleFlag then
- msgbox "Found Printer Group: " & objGroup
- end if
- objChildMemberOf = (GetObject("LDAP://" & objGroup)).MemberOf
- for Each objChildGroup in objChildMemberOf
- if (objChildGroup = "") then
- '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
- AddPrinter objChildMemberOf
- else
- AddPrinter objChildGroup
- end if
- next
- else
- 'If the group is not in the Printer Groups OU, pass it to AddPrinter function for analysis
- if bTroubleFlag then
- msgbox "Found non Printer Group: " & objGroup
- end if
- AddPrinter objGroup
- end if
- next
- 'Sets default printer based on group membership within the Printer Defaults OU
- for Each objGroup in objMemberOf
- if (instr(objGroup,"OU=Printer Defaults") <> 0) then
- set objGroupEnum = GetObject("LDAP://" & objGroup)
- if bTroubleFlag then
- msgbox "Setting Default:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description
- end if
- objNetwork.SetDefaultPrinter objGroupEnum.description
- set objGroupEnum = nothing
- end if
- next
- if bTroubleFlag then
- msgbox "Printer Logon Script Finished"
- end if
- Function AddPrinter(objIn)
- 'Function expects to be passed an LDAP string for a group
- 'Validates that an object is in the correct OU and attempt to find a UNC path in the description to install the printer.
- if (instr(objIn,"OU=Printer Paths") <> 0) then
- 'Bind to the group to get is description. The description contain the path to the printer
- set objGroupEnum = GetObject("LDAP://" & objIn)
- if bTroubleFlag then
- msgbox "Adding:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description
- end if
- objNetwork.AddWindowsPrinterConnection objGroupEnum.description
- set objGroupEnum = nothing
- end if
- end Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement