Quantumation

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

7 posts in this topic

#1 ·  Posted (edited)

#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



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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

How do you set a Topic as closed?

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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

    • squirrelc0de
      By squirrelc0de
      Hi there, 

      I have a question about persistent drives and AD. 
      I am playing around with a script but I'm missing something. What i want to do is if a user is part of an OU, it will map a network drive and be persistent. However if a user is moved out of that OU, they will need to have the persistent drive removed. 

      I'm using the ad plugin script, and i can map the drives if a user is in a specific ou, but i cannot seem to delete the drive if the user is out of the OU. 

      Here's an example of code I'm using: 

       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Compression=4 #AutoIt3Wrapper_Res_Fileversion=1.0.0 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <AD\AD.au3> func MapDrives() _AD_Open() if _AD_RecursiveIsMemberOf(OU) Then Mapdrive1() Elseif _AD_RecursiveIsMemberOf(different ou) drivemapdel EndIf _AD_Close() EndFunc Func MapDrive1() Drivemapdel ("Z:") DriveMapAdd ("Z:"."\\server\share",$DMA_PERSISTENT,0) EndFunc  
    • 31290
      By 31290
      Hi guys, 
      I'd like to write a piece of tool that would allow me to update a certain field in our Active Directory from a comma separated csv file composed like this:

      This file, automatically generated, can hold more than 10k lines.
      Thus, I need column A to be in one variable, column B in a second one and column C in a third one.
      I'm really missing this part as updating the AD is fairly easy once the 3 variable are populated. 
      I see things like this:
      Here's my attempts at the moment:
      #include <File.au3> #include <Array.au3> Global $csv_file = @DesktopDir & "\Book1.csv" Global $aRecords If Not _FileReadToArray($csv_file,$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf For $x = 1 to $aRecords[0] Msgbox(0,'Record:' & $x, $aRecords[$x]) ; Shows the line that was read from file $csv_line_values = StringSplit($aRecords[$x], ",",1) ; Splits the line into 2 or more variables and puts them in an array ; _ArrayDisplay($csv_line_values) ; Shows what's in the array you just created. ; $csv_line_values[0] holds the number of elements in array ; $csv_line_values[1] holds the value ; $csv_line_values[2] holds the value ; etc Msgbox(0, 0, $csv_line_values[1]) Next Any help on this please? 
      Thanks in advance
      -31290-
    • water
      By water
      Should the AD UDF support the fine grained password policy available since Windows Server 2012?
      What do fine-grained password policies do?
      You can use fine-grained password policies to specify multiple password policies in a single domain and apply different restrictions for password and account lockout policies to different sets of users in a domain.
    • water
      By water
      ADCU displays two Active Directory users and their group membership in two listviews. You can filter and export the data to Excel, Outlook mail and the clipboard.
      Before running the script you need to change file AD-Tools.ini and function _Check_Access in AD-Tools_User.au3.
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort
      Needs to be run with the latest AutoIt production version (>= 3.3.12.0).
      Needs to be run with the latest version of the AD UDF (>= 1.4.2.0).
    • water
      By water
      ADCG displays two Active Directory groups and their direct members in two listviews. You can filter and export the data to Excel, Outlook mail and the clipboard.
      Before running the script you need to change file AD-Tools.ini and function _Check_Access in AD-Tools_User.au3.
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort
      Needs to be run with the latest AutoIt production version (>= 3.3.12.0).
      Needs to be run with the latest version of the AD UDF (>= 1.4.2.0).