Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Active Directory Computer Groups in VBA

Active Directory Computer Groups in VBA

Scheduled Pinned Locked Moved Visual Basic
csharpwindows-adminquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AaronM_NZ
    wrote on last edited by
    #1

    So I have to do some code in VBA (if only it was .Net I wouldnt be here!) to get a list of all the groups a COMPUTER object is in from Active Directory. I can do this easily with ADS and a user, but the IADsComputer object does not have groups with it. Any idea how I could do this? The following works for users (in Excel)

    Sub testHarness()
    'Dont forget to add reference to Active DS Type Library

    Dim usr As ActiveDs.IADsUser
    Set usr = GetObject("WinNT://_<domainname>_/_<username>_,user")
        
    For Each oGroupIn usr.Groups()
        ActiveCell.Value = oGroup.Name
        ActiveCell.Offset(1, 0).Select
    Next
    

    End Sub

    I would think that the following would work for Computers, but alas it does not :mad:. I have been hunting for examples all day, but I can only find .net ones (which all work perfectly!)

    Sub testHarness()
    'Dont forget to add reference to Active DS Type Library

    Dim srv As ActiveDs.IADsComputer
    Set srv = GetObject("WinNT://_<domainname>_/_<servername>_,computer")
        
    For Each oGroup In srv.Groups()
        ActiveCell.Value = oGroup.Name
        ActiveCell.Offset(1, 0).Select
    Next
    

    End Sub

    Or do I need to do this completly differently? (And I really hope this is the right forum for VBA, I couldnt see anything else)

    modified on Tuesday, January 13, 2009 7:05 PM

    A 1 Reply Last reply
    0
    • A AaronM_NZ

      So I have to do some code in VBA (if only it was .Net I wouldnt be here!) to get a list of all the groups a COMPUTER object is in from Active Directory. I can do this easily with ADS and a user, but the IADsComputer object does not have groups with it. Any idea how I could do this? The following works for users (in Excel)

      Sub testHarness()
      'Dont forget to add reference to Active DS Type Library

      Dim usr As ActiveDs.IADsUser
      Set usr = GetObject("WinNT://_<domainname>_/_<username>_,user")
          
      For Each oGroupIn usr.Groups()
          ActiveCell.Value = oGroup.Name
          ActiveCell.Offset(1, 0).Select
      Next
      

      End Sub

      I would think that the following would work for Computers, but alas it does not :mad:. I have been hunting for examples all day, but I can only find .net ones (which all work perfectly!)

      Sub testHarness()
      'Dont forget to add reference to Active DS Type Library

      Dim srv As ActiveDs.IADsComputer
      Set srv = GetObject("WinNT://_<domainname>_/_<servername>_,computer")
          
      For Each oGroup In srv.Groups()
          ActiveCell.Value = oGroup.Name
          ActiveCell.Offset(1, 0).Select
      Next
      

      End Sub

      Or do I need to do this completly differently? (And I really hope this is the right forum for VBA, I couldnt see anything else)

      modified on Tuesday, January 13, 2009 7:05 PM

      A Offline
      A Offline
      AaronM_NZ
      wrote on last edited by
      #2

      I have found the answer, based on this post - http://www.tek-tips.com/viewthread.cfm?qid=1245332&page=1[^] My resulting code now looks like this, in case anyone else is interested:

      Sub TestHarness
      Set objTrans = CreateObject("NameTranslate")

      objTrans.Set 3, "_<domainname>\\<servername>_$"
      strComputerDN = objTrans.Get(1)
      
      Set objComputer = GetObject("LDAP://" & strComputerDN)
      colGroups = objComputer.MemberOf
      
      For i = 0 To UBound(colGroups)
          ActiveCell.Value = GetGroup(colGroups(i))
          ActiveCell.Offset(1, 0).Select
      Next
      

      End Sub

      Function GetGroup(strGroup) As String
      z = Split(strGroup, ",")
      If Left(z(0), 3) = "CN=" Then
      GetGroup = Right(z(0), Len(z(0)) - 3)
      Else
      GetGroup = ""
      End If
      End Function

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups