Active Directory Computer Groups in VBA
-
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 LibraryDim 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 LibraryDim 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
-
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 LibraryDim 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 LibraryDim 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
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