Jump to content
Quantumation

[Resolved]How to sort _AD_GetUserGroups to import back into AD

Recommended Posts

Quantumation
#RequireAdmin
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <AD.au3>
#include <Array.au3>
#include <GuiButton.au3>
#include <String.au3>

#Region ### START Koda GUI section ### Form=
Global $Form1_1 = GUICreate("Get User Groups", 419, 501, -1, -1)
Global $Username = GUICtrlCreateInput("", 176, 80, 121, 21)
Global $Button1 = GUICtrlCreateButton("GetGroups", 24, 120, 75, 25)
Global $Groups = GUICtrlCreateEdit("", 24, 168, 369, 313, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetData(-1, "")
GUICtrlSetData(-1, "")
Global $Label2 = GUICtrlCreateLabel("Copy and paste the text below and add it to the onboarding ticket.", 80, 40, 318, 17)
Global $Cancel = GUICtrlCreateButton("Cancel", 312, 120, 75, 25)
Global $Disable = GUICtrlCreateButton("Copy", 120, 120, 75, 25)
Global $Expire = GUICtrlCreateButton("Expire", 216, 120, 75, 25)
Global $Unique = GUICtrlCreateLabel("Unique Username", 56, 80, 89, 25)
GUISetState(@SW_SHOW)
WinActivate($Form1_1)
_GUICtrlButton_Enable($Username)

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            GroupArray()
        Case $Disable
            Copy()
        Case $Cancel
            Exit
    EndSwitch
WEnd

;~ Functions

Func GroupArray()
    _AD_Open()
    Global $Inputbox = GUICtrlRead($Username)
    If @error Then Exit MsgBox(16, "Active Directory", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
    ; Get a sorted array of group names (FQDN) that the user is immediately a member of
    Global $aUser = _AD_GetUserGroups($Inputbox)
    If @error > 0 Then
        MsgBox(64, "Active Directory Function", "User '" & $Inputbox & "' has not been assigned to any group or cannot be found.")
    Else
        _ArraySort($aUser, 0, 1)
        $sString = _ArrayToString($aUser, "; ")
        Global $sorted = _StringBetween($sString, "CN=", ",")
        Global $Format = _ArrayToString($sorted, "; ")
        Guictrlsetdata($Groups, $Format)
    EndIf
    ; Close Connection to the Active Directory
    _AD_Close()
EndFunc   ;==>GroupArray

Func Copy()
    $copy = GUICtrlRead($groups)
    ClipPut($copy)
EndFunc

I've found this to be very useful but It only shows the names of the groups in FDQN format.

In order to import multiple groups back into AD The have to be formatted like so:

Domain users; Finance; Domain Admins;

This allows you to copy and paste that back into an AD account where you need add multiple groups to one user.

I've written something to clean it up a bit but i'm new to autoit. I just started like 2 weeks ago and i'm not sure how to sort info. The script i've written allows you to take the appropriate info out but it take a little too much info out. I'm using string between and I'd like to know if there is a way to extract info better from the array used in _AD_GetUserGroups

I've attached my script and GUI but it pulls too much info due to the _stringbetween function. I just need to know if there is a better way?

 

 

 

GetGroups.au3

Edited by Quantumation

Share this post


Link to post
Share on other sites
water

It takes some time but you could loop throught the array and run

$aSam = _AD_GetObjectAttribute($aUser[$i], "samaccountname")

for every entry to get the samaccountname of the group.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Quantumation

Thank you water for your quick reply but I found a solution that's a little quicker.

Func GroupArray()
    GUICtrlSetData($Groups, "")
    _AD_Open()
    Global $Inputbox = GUICtrlRead($Username)
    If @error Then Exit MsgBox(16, "Active Directory", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
    ; Get a sorted array of group names (FQDN) that the user is immediately a member of
    Global $aUser = _AD_GetUserGroups($Inputbox)
    If @error > 0 Then
        MsgBox(64, "Active Directory Function", "User '" & $Inputbox & "' has not been assigned to any group or cannot be found.")
    Else
        _ArraySort($aUser, 0, 1)

; HERE IS WHERE I HAD TO FIX!!!!!!!!!!!!!!!!!!!!
        $sString = _ArrayToString($aUser, ";")
        Global $sorted = _StringBetween($sString, ";CN=", ",")
        Global $Format = _ArrayToString($sorted, "; ")
        GUICtrlSetData($Groups, $Format)
    EndIf
    ; Close Connection to the Active Directory
    _AD_Close()
EndFunc   ;==>GroupArray

When you do an _arraytostring you use  a Delimiter, you'll see that I used ";"

This separates each row with the semicolon so that each line starts with ";CN="

Then all if you edit the string between real quick to:

Global $sorted = _StringBetween($sString, ";CN=", ",")

That brings back every group name separated by the semicolon and a space so that you can copy it back into AD and every group will resolve.

This works because the first line in the original array tells you the amount of groups the user has instead of a group.

Hopefully this helps someone other than myself.

Share this post


Link to post
Share on other sites
Quantumation

How do you set a Topic as closed?

Share this post


Link to post
Share on other sites
water

Edit the first post and prefix the title with "[Closed]".
But maybe you need to have a minimum number of posts to do so (about 5 or 10). I'm not sure about that.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Quantumation

@water

Thank you for you help.

Share this post


Link to post
Share on other sites
water

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • antmar904
      By antmar904
      I'm trying to read all cells used in column "C" in excel to an array but not sure how.
       
      Local $NameArray = _Excel_RangeRead($oWorkbook, $oWorkbook.Activesheet, $oWorkbook.Range["C"].End)  
    • water
      By water
      Extensive library to control and manipulate Microsoft Active Directory.

      Threads: Development - General Help & Support - Example Scripts - Wiki
      Previous downloads: 30467
       
      Known Bugs: (last changed: 2018-08-08)
      _AD_IsPasswordExired: It compares the expiration time (UTC - but should be local time) with the current time (local time). Which is 2 hours off here. Will be fixed in the next version.
      Or replace line If $aTemp[11] <= _NowCalc() Then Return 1 with
      If $aTemp[9] <= _NowCalc() Then Return 1  
      Things to come: (last changed: 2018-08-08)
      Internal function to convert Large Integer to Date String. _AD_ModifyAttribute fully supports single-value and multi-value attributes. Returns better error information. Better support for Fine Grained Password Policy
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort
    • Blois
      By Blois
      Hey Guys,
      Good?
      I'm ned help to consult in other domain. My three domain contains any domains.
      How do I get this query done?
       
      Tks for the Help!
       
×