• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Script to add Printer when user is added to specific group in AD

I am trying to write a SB script that will allow me to add a printer when a user is added to a specifc group in active Directory.

Refering to my Code below what I would like to happen is when a user is added to:

Group1  --> Map Printer KM_5050
Group2 --> Add Printer KM_3035
Group3 --> Add Printer KM_2550

when a user is added to group 1 and 2 they would receive both printers

Is there anyone who might be able to tell me where my code went askew?
<job id="vbs">
<script language="VBScript">
Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim strUserLocation, strLocationType, strUserDomain, strFollettGroup, strUserAppServer, strUserDesktop
strUserDomain = "Domain.lcl"
On Error Resume Next
'***** New Printer Map for Kyocera Main Office
If getGroupMembership(strUserDomain,WshNetwork.UserName,"Group1") Then
            WshNetwork.AddWindowsPrinterConnection "\\server3\Kyocera KM-5050 (main office)"
else If
'***** New Printer Map for Kyocera for ASP
If getGroupMembership(strUserDomain,WshNetwork.UserName,"Group2") Then
            WshNetwork.AddWindowsPrinterConnection "\\Server3\Kyocer KM-3035 (ASP)"
else If
'***** New Printer Map for Kyocera for Shared Houseing
If getGroupMembership(strUserDomain,WshNetwork.UserName,"Group3") Then
            WshNetwork.AddWindowsPrinterConnection "\\Server3\Kyocera KM-2550 (SH)"
Function getGroupMembership (strDomain,strUser,strGroupMem)
	suserobj = "windows://" & strDomain & "/" & strUser
	set suser = getobject(suserobj)
	For Each x in suser.groups
		If x.name = strGroupMem Then
			getGroupMembership = TRUE
			Exit Function
		End If
	getGroupMembership = FALSE
End Function
Set wshNetwork = Nothing

Open in new window

1 Solution
Try this instead...
Edit line 16, 18 and 20 to match your group names.

If this does not map the printers, then the printer share name might be different.
Set WSHNetwork = WScript.CreateObject("WScript.Network")
On Error Resume Next
Domain = WSHNetwork.UserDomain
UserName = ""
While UserName = ""
	UserName = WSHNetwork.UserName
	MyGroups = GetGroups(Domain, UserName)
Set objUser = GetObject("WinNT://" & Domain & "/" & UserName)
For Each objGroup In objUser.Groups
	select Case objGroup.Name
			MapPrinter "\\server3\Kyocera KM-5050 (main office)", "FALSE"
			MapPrinter "\\Server3\Kyocer KM-3035 (ASP)>", "FALSE"
			MapPrinter "\\Server3\Kyocera KM-2550 (SH)", "FALSE"
	end select
Sub MapPrinter(sPrinterPath,sPrinterDefault)
	On Error Resume Next
	WSHNetwork.AddWindowsPrinterConnection sPrinterPath
	WScript.Sleep 300
	If sPrinterDefault = "1" Or sPrinterDefault = UCase("TRUE") Then
		WSHNetwork.SetDefaultPrinter sPrinterPath
	End If
End Sub

Open in new window

itvizionAuthor Commented:
Thanks this was exactly what i needed

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now