Get user information Active Diretory VBA + AHK

Discuss other programming languages besides AutoHotkey
User avatar
carlosatchim2103
Posts: 17
Joined: 05 Feb 2015, 16:09
Location: São Paulo - Brazil

Get user information Active Diretory VBA + AHK

Post by carlosatchim2103 » 07 Feb 2015, 13:28

Hello friends, I want to make the AutoHotkey harvest user information in Active Directory.

I have no knowledge of VBA and just dare to program something in AHK (is the only one I do some work cpodigo)

What I need is the following, enter the user Login (userPrincipalName), using an InputBox
Image

Then he brings the following information in a MsgBox
Image

Full_name = cn
Site = company
City = l
Email = mail

A friend made in Excel with VBA, however, did not know anything about VBA and not know how to put that information collected on variables.

Below is what I tried to do without success:

Code: Select all

F5::
InputBox,userPrincipalName,login,
userPrincipalName = %userPrincipalName%@empresa.corp   ;@empresa.corp is domain of AD
code =
(
Atctive_DirectoryQuery(ByVal TypeFilter As String, ByVal InformationSought As String, ByVal SearchResult As String) As String

    ; Get the domain string ("dc=domain, dc=local")
    strDomain := ComObjGet("LDAP://rootDSE").Get("defaultNamingContext")
    
    ; ADODB Connection to AD
    objConnection := ComObjCreate("ADODB.Connection")
    objConnection.Open("Provider=ADsDSOObject")
        
    ; Connection
    objCommand := ComObjCreate("ADODB.Command")
    objCommand.ActiveConnection := objConnection
        
    ; Search the AD recursively, starting at root of the domain
    objCommand.CommandText := "<LDAP://" . strDomain . ">;(&(objectCategory=User)" . "(" . TypeFilter . "=" . InformationSought . "));" . TypeFilter . "," . SearchResult . ";subtree"

    ; RecordSet
    objRecordSet := objCommand.Execute

    If (objRecordSet.RecordCount = 0)
        ;return "No Data"  ; no records returned
    Else
    {
        return objRecordSet.Fields(SearchResult)  ; return value
    }

    ; Close connection
    objConnection.Close
)
MsgBox % ws.Eval("SearchResult")

User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Get user information Active Diretory VBA + AHK

Post by TheDewd » 27 May 2015, 11:43

You'll need to configure this and customize to your specific requirements... Hopefully it will help you!
Let me know if you need more help with this one.

Code: Select all

#SingleInstance, Force

objConnection := ComObjCreate("ADODB.Connection")
objCommand := ComObjCreate("ADODB.Command")
objRecordset := ComObjCreate("ADODB.Recordset")
strDomain := ComObjGet("LDAP://rootDSE").Get("defaultNamingContext")
objConnection.Open("Provider=ADsDSOObject;")
objCommand.ActiveConnection := objConnection

fieldList := "givenName,sn,displayName,SAMAccountName,mail,telephoneNumber,department,st,employeeID"

objCommand.CommandText := "SELECT " fieldList " From 'LDAP://" strDomain "' WHERE samAccountType='805306368'"
objCommand.Properties("Page Size").value := 4000

objRecordset := objCommand.Execute()

Loop
{
	If (objRecordset.eof = -1)
	{
		Break
	}
	
	If (objRecordset.Fields("givenName").value = "PERSON_NAME")
	{
		MsgBox, % objRecordset.Fields("givenName").value
		MsgBox, % objRecordset.Fields("sn").value
		MsgBox, % objRecordset.Fields("displayName").value
		MsgBox, % objRecordset.Fields("SAMAccountName").value
		MsgBox, % objRecordset.Fields("mail").value
		MsgBox, % objRecordset.Fields("telephoneNumber").value
		MsgBox, % objRecordset.Fields("department").value
		MsgBox, % objRecordset.Fields("st").value
		MsgBox, % objRecordset.Fields("employeeID").value
	}
	objRecordset.MoveNext()
}
Return

User avatar
hoppfrosch
Posts: 449
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Get user information Active Diretory VBA + AHK

Post by hoppfrosch » 29 May 2015, 00:25

That's what I have on AD-functionality yet - there once was a thread about ActiveDirectory on the old AHK-forum, but I cannot find it anymore ...

All credits for the script go to an other guy, which I cannot remember his name. Maybe you can take these functions as template:

Code: Select all

name := FindDistinguishedName("jok")

MsgBox % name

groups := GetMembersOfADGroup("NTSoftware")

MsgBox % groups

groups := UserIsMemberOf("jok")

MsgBox % groups

UserIsMemberOf(_User)
{

   StringLeft, UserNameStart, _User, 3
   StringUpper, UserNameStart, UserNameStart
   If UserNameStart != "CN=" ; We were given a simple name for the group so we find the distinguished name.
   {
      UserName := FindDistinguishedName(_User)

   } else {
      UserName := %_User%
   }
   
   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
   objCommand.CommandText := "<" . strADPath . ">" . ";(&(&(&(objectCategory=group)(member=" . UserName . "))));Name;subtree"
   objRecordSet := objCommand.Execute
   objRecordCount := objRecordSet.RecordCount
   objOutputVar :=
   While !objRecordSet.EOF
   {
      strObjectDN := objRecordSet.Fields.Item("Name").value
      a = %a%`n%strObjectDN%
      objRecordSet.MoveNext
   }
   objRelease(objRootDSE)
   objRelease(objDomain)
   objRelease(objConnection)
   objRelease(objCommand)
   return a
}

FindDistinguishedName(_Item)
{
   ;This finds a full DN name from a short name or a samaccount name.
   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

   objCommand.CommandText := "<" . strADPath . ">;(|(name=" . _Item . ")(sAMAccountName=" . _Item . "));distinguishedName;subtree"
   objRecordSet := objCommand.Execute
   objRecordCount := objRecordSet.RecordCount
   objOutputVar :=
   While !objRecordSet.EOF
   {
      _Item := objRecordSet.Fields.Item("distinguishedName").value
      objRecordSet.MoveNext
   }
   objRelease(objRootDSE)
   objRelease(objDomain)
   objRelease(objConnection)
   objRelease(objCommand)
   return _Item
}


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
}

IsUserOfGroup(_User,_Group, _Recurse=0)
{
   ; First we check the obvious first level of the group.
   Users := GetMembersOfADGroup(_Group, "user")
   StringSplit, Users, Users,`n
   Loop, %Users0%
   {
      If Users%a_index% = %_User%
      {
         return true
      }
   }
   
   If _Recurse
   {
      ; User was not in first level of group and since the recursive flag is set, we have to go deeper.
      ; This is limited to only one recursion for now. It will check all the groups in this group for the user.
      GroupIndex = 0
      
      Groups := GetMembersOfADGroup(_Group, "group")
      StringSplit, GroupsArray, Groups,`n
      If GroupsArray0
      {
         Loop
         {
            GroupIndex++
            If GroupIndex > %GroupsArray0%
            {
               return false
            }
            
            ;Look for the user in the current group in the list.
            CurGroup := GroupsArray%GroupIndex%
            Users := GetMembersOfADGroup(CurGroup, "user")
            StringSplit, Users, Users,`n
            Loop, %Users0%
            {
               If Users%a_index% = %_User%
               {
                  return true
               }
            }
         }
      }
      return false
   }
   ; We couldn't find the user.
   return false
}


Post Reply

Return to “Other Programming Languages”