AutoHotkey Community

It is currently May 27th, 2012, 8:15 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 

Would you like me to post more?
YES PLEASE!
No, your code is crap.
You may select 1 option

View results
Author Message
PostPosted: January 14th, 2012, 6:57 am 
Offline

Joined: January 14th, 2012, 6:51 am
Posts: 5
Location: Columbus, OH
Here is some code I wrote to determine if a user is in a group or not in active directory (somewhat recursively).

** Based off some examples from users like lsavell and others.... **


Code:
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 16th, 2012, 7:36 am 
Offline
User avatar

Joined: January 25th, 2006, 8:08 am
Posts: 225
Location: Froschtümpel
What I would like to see first:

Code:
UserIsMemberOf(_Username)



which gives a list of groups of which a given user is member of ...

Other functions which might be useful:
  • List all available groups
  • List all availabl users
Those would allow to create a GUI with pre-filled selection boxes ...


_________________________

Code:
;     (.)~(.)   
;    (-------)                                   
;---ooO-----Ooo---------------------------------------------------
;    Hoppfrosch  - AHK 1.1.05.06 Unicode 32bit on Win7 Ultimate
;-----------------------------------------------------------------                       
;    ( )   ( )                           
;    /|\   /|\ 


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2012, 8:59 pm 
Offline

Joined: May 18th, 2008, 9:39 pm
Posts: 27
Xanthus, thanks for your code. I'am still using the following VBS script to get all nested groups from the user running the VBS script. I know it is possible to translate this VBS code to Autohotkey but unfortunatly i am not getting the results (nested groups) i want in autohotkey. The 'IsUserOfGroup' function is working ok for this but it will not get a list of all the nested groups.

Code:
Option Explicit

Dim objSysInfo, objGroupList, objUser, strDN, MemberOf

Set objSysInfo = CreateObject("ADSystemInfo")
strDN = objSysInfo.UserName

Set objUser = GetObject("LDAP://" & strDN)

' Bind to dictionary object.
Set objGroupList = CreateObject("Scripting.Dictionary")

' Enumerate group memberships.
Call EnumGroups(objUser)
Wscript.Echo MemberOf

' Clean up.
Set objGroupList = Nothing
Set objUser = Nothing

Sub EnumGroups(ByVal objADObject)
    Dim colstrGroups, objGroup, j
    objGroupList.CompareMode = vbTextCompare
    colstrGroups = objADObject.memberOf
    If (IsEmpty(colstrGroups) = True) Then
        Exit Sub
    End If
    If (TypeName(colstrGroups) = "String") Then
        colstrGroups = Replace(colstrGroups, "/", "\/")
        Set objGroup = GetObject("LDAP://" & colstrGroups)
        If (objGroupList.Exists(objGroup.sAMAccountName) = False) Then
            objGroupList.Add objGroup.sAMAccountName, True
          
         MemberOf = MemberOf & objGroup.CN & Chr(13)
         
            Call EnumGroups(objGroup)
        End If
        Set objGroup = Nothing
        Exit Sub
    End If
    For j = 0 To UBound(colstrGroups)
        colstrGroups(j) = Replace(colstrGroups(j), "/", "\/")
        Set objGroup = GetObject("LDAP://" & colstrGroups(j))
        If (objGroupList.Exists(objGroup.sAMAccountName) = False) Then
            objGroupList.Add objGroup.sAMAccountName, True
          
         MemberOf = MemberOf & objGroup.CN & Chr(13)
         
            Call EnumGroups(objGroup)
        End If
    Next
    Set objGroup = Nothing
End Sub


Do you know if it is possible to translate this code or part of this code to Autohotkey?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 23rd, 2012, 1:43 am 
Offline

Joined: January 14th, 2012, 6:51 am
Posts: 5
Location: Columbus, OH
This needs the function I posted earlier to find the CN of a short username called FindDistinguishedName(_User).


Code:
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
}


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 23rd, 2012, 7:25 am 
Offline
User avatar

Joined: January 25th, 2006, 8:08 am
Posts: 225
Location: Froschtümpel
Xanthus wrote:
Code:
UserIsMemberOf(_User) ...


Thanks! Works as expected ....


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 13th, 2012, 9:51 pm 
Offline

Joined: January 14th, 2012, 6:51 am
Posts: 5
Location: Columbus, OH
Please look at http://github.com/jwhipple for the latest code.

JRWTools.ahk in Libraries is what most people will find an interest in.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Aravind, Stigg and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group