Query AD Group

Get help with using AutoHotkey and its commands and hotkeys
Zemich
Posts: 4
Joined: 06 May 2016, 05:46

Query AD Group

06 May 2016, 06:08

Hi Guys,

I have been given a bit of a complicated task. Well, for me it is anyway.

I am looking for a way to query an Active Directory group for computer accounts, export the computer names to text files and append the full domain name to each computer name.
There is a limit of max 200 computers in each text file, and it has to be in alphabetical order.

I hope someone can help me with this task.
User avatar
kczx3
Posts: 1197
Joined: 06 Oct 2015, 21:39

Re: Query AD Group

06 May 2016, 09:16

Maybe? This comes from an old coworkers library but I've never personally used it.

Code: Select all

GetMembersOfADGroup(_Group, _Type = "both") ; _Type can be user (return only users of the group), group (return only groups in this group) or both. 
{
	MembersOfGroup := Object()
	objRootDSE := ComObjGet("LDAP://rootDSE")
	strDomain := objRootDSE.Get("defaultNamingContext")
	strADPath := "LDAP://" . strDomain
	objDomain := ComObjGet(strADPath)
	objConnection := ComObjCreate("ADODB.Connection")
	objConnection.Open("Provider=ADsDSOObject")
	objCommand := ComObjCreate("ADODB.Command")
	objCommand.ActiveConnection := objConnection
	
	StringLeft, GroupNameStart, _Group, 3
	StringUpper, GroupNameStart, GroupNameStart
	If GroupNameStart != "CN=" ; We were given a simple name for the group so we find the distinguished name.
	{
		_Group := FindDistinguishedName(_Group)
	}
	
	LDAPSearchString := "<" . strADPath . ">;"
	
	IfEqual, _Type,user
	{
		LDAPSearchString .= "(&(&(&(memberOf=" . _Group . ")(objectCategory=user)(objectClass=user))))"
	}
	
	IfEqual, _Type,group
	{
		LDAPSearchString .= "(&(objectCategory=group)(memberOf=" . _Group . "))"
	}
	
	IfEqual, _Type,both
	{
		LDAPSearchString .= "(memberOf=" . _Group . ")"
	}
	
	LDAPSearchString .= ";sAMAccountName;subtree"
	objCommand.CommandText := LDAPSearchString
	
	objRecordSet := objCommand.Execute
	objRecordCount := objRecordSet.RecordCount
	objOutputVar :=
	x = 0
	MemberGroups =
	
	While !objRecordSet.EOF
	{
		sAMAccountName := objRecordSet.Fields.Item("sAMAccountName").value
		;sAMAccountType := objRecordSet.Fields.Item("sAMAccountType").value
		If MemberGroups
		{
			MemberGroups := MemberGroups . "`n"
		}
		MemberGroups := MemberGroups . sAMAccountName		
		objRecordSet.MoveNext
	}

	objRelease(objRootDSE)
	objRelease(objDomain)
	objRelease(objConnection)
	objRelease(objCommand)
	return MemberGroups
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot] and 32 guests