Jump to content

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


Recommended Posts

#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
Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

×
×
  • Create New...