I want to add printers by groups

networking Netherlands
  • 16 years ago

    i've got a problem...


    I've created several groups of computers, and each computer should be assigned to a specific network-printer.. the assignment will be done according to groupmembership of a computer, so far i made the following script:


    Everything in red is added by myself


    '==========================================================================
    '
    ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1
    '
    ' NAME: AddPrinter.VBS
    '
    ' AUTHOR: Mathijs Beckers , Het Kwdrant
    ' DATE  : 15-3-2004
    '
    ' COMMENT: Functie IsMember komt van : http://www.rlmueller.net/IsMember6.htm
    '           Dit script bekijkt of een computer lid is van een bepaalde groep
    '           en installeerd vervolgens de printers die bij de desbetreffende
    '           groep computers hoort.
    '==========================================================================



    Option Explicit


    Dim objADObject1, objADObject2, strGroup
    Dim objGroupList, objCommand, objConnection, objRootDSE
    Dim objRecordSet, strAttributes, strFilter, strQuery
    Dim oNet


    ' Bind to user objects in Active Directory with the LDAP provider.
    Set objADObject1 = GetObject ("LDAP://dc=HetKwadrant.proeftuin" )


    ' Maak een netwerkobject aan om de printer connectie te kunnen maken
    Set oNet = CreateObject("Wscript.Network")


    On Error Resume next


    strGroup = "printer"
    If IsMember(objADObject1, strGroup) Then
       oNet.AddPrinterConnection("\PROEFSVR\PROEF")
    End If


    Function IsMember(objADObject, strGroup)
    ' Function to test for group membership.
    ' objADObject is a user or computer object.
    ' strGroup is the NT name (sAMAccountName) of the group to test.
    ' objGroupList is a dictionary object, with global scope.
    ' ADO is used to retrieve all group objects from the domain, with
    ' their PrimaryGroupToken. Each objADObject has a PrimaryGroupID.
    ' The group with the matching PrimaryGroupToken is the primary group.
    ' Returns True if the user or computer is a member of the group.
    ' Subroutine LoadGroups is called once for each different objADObject.


     Dim strPrimaryGroup
     Dim intPrimaryGroupToken, intPrimaryGroupID


     If IsEmpty(objGroupList) Then
    ' Create dictionary object.
       Set objGroupList = CreateObject("Scripting.Dictionary")
       objGroupList.CompareMode = vbTextCompare


    ' Use ADO to retrieve all group "primaryGroupToken" values.
       Set objConnection = CreateObject("ADODB.Connection")
       Set objCommand = CreateObject("ADODB.Command")
       objConnection.Provider = "ADsDSOObject"
       objConnection.Open "Active Directory Provider"
       Set objCommand.ActiveConnection = objConnection
       objCommand.Properties("Page Size") = 100
       objCommand.Properties("Timeout") = 30
       objCommand.Properties("Cache Results") = False
       strAttributes = "sAMAccountName,primaryGroupToken"
       Set objRootDSE = GetObject("LDAP://RootDSE")
       strDNSDomain = objRootDSE.Get("defaultNamingContext")
       strFilter = "(objectCategory=group)"
       strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter & ";" _
         & strAttributes & ";subtree"
       objCommand.CommandText = strQuery
       Set objRecordSet = objCommand.Execute
     End If
     If Not objGroupList.Exists(objADObject.sAMAccountName & "\") Then
    ' Call LoadGroups for each different objADObject.
    ' Add object name to dictionary object so groups need only be
    ' enumerated once.

       Call LoadGroups(objADObject, objADObject)
       objGroupList(objADObject.sAMAccountName & "\") = True


    ' Determine which group is the primary group for this object.
       intPrimaryGroupID = objADObject.primaryGroupID
       objRecordSet.MoveFirst
       Do Until objRecordSet.EOF
         intPrimaryGroupToken = objRecordSet("primaryGroupToken")
         If intPrimaryGroupToken = intPrimaryGroupID Then
           strPrimaryGroup = objRecordSet.Fields("sAMAccountName")
           objGroupList(objADObject.sAMAccountName & "\" _
             & strPrimaryGroup) = True
           Exit Do
         End If
         objRecordSet.MoveNext
       Loop
     End If


    ' Check group membership.
     IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
       & strGroup)
    End Function


    Sub LoadGroups(objPriADObject ,objSubADObject)
    ' Recursive subroutine to populate dictionary object with group
    ' memberships. When this subroutine is first called by Function
    ' IsMember, both objPriADObject and objSubADObject are the user or
    ' computer object. On recursive calls objPriADObject still refers to the
    ' user or computer object being tested, but objSubADObject will be a
    ' group object. The dictionary object objGroupList keeps track of group
    ' memberships for each user or computer separately.
    ' For each group in the MemberOf collection, first check to see if
    ' the group is already in the dictionary object. If it is not, add the
    ' group to the dictionary object and recursively call this subroutine
    ' again to enumerate any groups the group might be a member of (nested
    ' groups). It is necessary to first check if the group is already in the
    ' dictionary object to prevent an infinite loop if the group nesting is
    ' "circular". The MemberOf collection does not include any "primary"
    ' groups.


     Dim colstrGroups, objGroup, j
     colstrGroups = objSubADObject.memberOf
     If IsEmpty(colstrGroups) Then
       Exit Sub
     End If
     If TypeName(colstrGroups) = "String" Then
       Set objGroup = GetObject("LDAP://" & colstrGroups)
       If Not objGroupList.Exists(objPriADObject.sAMAccountName & "\" _
           & objGroup.sAMAccountName) Then
         objGroupList(objPriADObject.sAMAccountName & "\" _
           & objGroup.sAMAccountName) = True
         Call LoadGroups(objPriADObject, objGroup)
       End If
       Set objGroup = Nothing
       Exit Sub
     End If
     For j = 0 To UBound(colstrGroups)
       Set objGroup = GetObject("LDAP://" & colstrGroups(j))
       If Not objGroupList.Exists(objPriADObject.sAMAccountName & "\" _
           & objGroup.sAMAccountName) Then
         objGroupList(objPriADObject.sAMAccountName & "\" _
           & objGroup.sAMAccountName) = True
         Call LoadGroups(objPriADObject, objGroup)
       End If
     Next
     Set objGroup = Nothing
    End Sub


    '==========================================================================



    i believe there's a simpler way to achieving that which i'm trying to get...
    A computer will get a printer because it's in a certain group (the computer is in the group)...

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay