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?
<package>
 
<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)"
endif
'*****************************************************************************************
 
'Functions
 
 
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
	Next
	getGroupMembership = FALSE
End Function
 
Set wshNetwork = Nothing
 
 
</script>
 
</job>
 
</package>

Open in new window

itvizionAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rejoinderCommented:
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)
Wend
 
Set objUser = GetObject("WinNT://" & Domain & "/" & UserName)
For Each objGroup In objUser.Groups
	select Case objGroup.Name
		Case "GROUPNAME1"
			MapPrinter "\\server3\Kyocera KM-5050 (main office)", "FALSE"
		Case "GROUPNAME2"
			MapPrinter "\\Server3\Kyocer KM-3035 (ASP)>", "FALSE"
		Case "GROUPNAME3"
			MapPrinter "\\Server3\Kyocera KM-2550 (SH)", "FALSE"
	end select
Next
 
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itvizionAuthor Commented:
Thanks this was exactly what i needed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.