Share via


Adding Network Printers based on group memberships using Logon Script and display a MessageBox

This VBScript will determine all the security groups that the currently logged on user is member of and add network printers accroding to your conditions set in this script. This script will show a MessageBox Popup to display the summery.

01.'=======================================================================================
02.'Name: AddNetworkPrinters.vbs
03.'Version: v1.00
04.'Author: Touhidul Islam
05.'Created on: 19/10/2015 13:36
06.'Purpose: To add Network Printers during logon based on user's Security Group membership
07.'=======================================================================================
08. 
09.'start script
10. 
11.On Error Resume Next
12.Dim objSysInfo, objNetwork, objUser, objGroup
13.Set objSysInfo = CreateObject("ADSystemInfo")
14.Set objNetwork = CreateObject("WScript.Network")
15.Set objShell = WScript.CreateObject("WScript.Shell")
16. 
17. 
18.'Find the currently logged in User
19. 
20.Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
21. 
22.'Find all Security Groups that this user is Member Of
23. 
24.For Each strGroup in objUser.MemberOf
25. 
26.    strGroupPath = "LDAP://" & strGroup
27.    Set objGroup = GetObject(strGroupPath)
28.    strGroupName = objGroup.CN
29. 
30.    'Add Network Printers based on security groups
31. 
32.    Select Case strGroupName
33. 
34.        Case "Test Printer TFloor"
35.        objNetwork.AddWindowsPrinterConnection "\\PRINT-SERVER\Printer 1"
36.        objNetwork.AddWindowsPrinterConnection "\\PRINT-SERVER\Printer 2"
37.        objNetwork.AddWindowsPrinterConnection "\\PRINT-SERVER\Printer 3"
38.        objNetwork.SetDefaultPrinter "\\PRINT-SERVER\Printer 1"
39. 
40.    End Select
41. 
42.Next
43. 
44.'Create a list of Network Printers on this Computer
45. 
46.strComputer = "."
47. 
48.Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
49. 
50.Set colPrinters = objWMIService.ExecQuery _
51.    ("Select * From Win32_Printer Where Local = FALSE")
52.     
53.'Create a output string with the list
54.     
55.If colPrinters.count = 0 Then
56.    strOutput = "Unfortunately no printers were found." & vbCrLf
57.Else
58.For Each objPrinter in colPrinters
59.    strOutput = strOutput & objPrinter.Name & vbCrLf
60.Next
61.End If
62. 
63.'Display the output using a VBS MessageBox
64. 
65.op = objShell.Popup("Logon Script 'AddNetworkPrinters.vbs' has been executed successfully." & vbCrLf & "Following Network Printers have been added to your Computer:" & vbCrLf & "" & vbCrLf & strOutput & vbCrLf & "Thank you!" ,6, "Logon Script Summery",vbInformation)
66. 
67.'end script