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

Posted on 2008-11-14
Last Modified: 2012-05-05
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 = strGroupMem Then

			getGroupMembership = TRUE

			Exit Function

		End If


	getGroupMembership = FALSE

End Function

Set wshNetwork = Nothing




Open in new window

Question by:itvizion
    LVL 14

    Accepted 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
    		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
    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


    Author Closing Comment

    Thanks this was exactly what i needed

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now