Jump to content

Active Directory Return Organizational Unit


Legacy99
 Share

Recommended Posts

Ive gotten this far but I cant seem to figure out what I need to finish. Ive got a script that will sniff out stale PCs in AD and delete them depending on what I set the password age to, that part works. The problem is the OU has to be hard coded so it's only good in this environment so I would like to add this custom search feature. I found an ADTool script (Author = myName?) in the forums that will open AD in a list view and I used this portion of it (attached). What I want to do is hit the save button and it pass the OU path on to the script that does the search in the CN=OU, DC=Domain, DC=Domain format. I have not done much/anything with GUI so I'm kinda treading new water here.

#include <GuiConstants.au3>
#include <adfunctions.au3>
#Include <GuiTreeView.au3>

Dim $objConnection, $objRootDSE, $objRecordSet, $arrContainers
$objConnection = ObjCreate("ADODB.Connection")  ; Create COM object to AD
$objCommand = ObjCreate("ADODB.Command")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")  ; Open connection to AD
$objCommand.ActiveConnection = $objConnection
$objCommand.Properties ("Page Size") = 1000
$objCommand.Properties ("Searchscope") = 2
$objRootDSE = ObjGet("LDAP://RootDSE")
Global $strDNSDomain = $objRootDSE.Get ("defaultNamingContext")  ; Retrieve the current AD domain name

$ADToolGUI = GUICreate("Active Directory Tool", 400, 680)
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

$Btn_SaveOU = GUICtrlCreateButton("<--- Search this OU", 225, 10, 160, 20)
GUICtrlSetOnEvent($Btn_SaveOU, "SaveOU")

$Treeview = GUICtrlCreateTreeView(10, 10, 200, 656, BitOR($GUI_SS_DEFAULT_TREEVIEW, $WS_BORDER), $WS_EX_CLIENTEDGE)
_ADGetObjectsInOU ($arrContainers, $strDNSDomain, "ObjectClass='organizationalUnit'", 1, "name,ADsPath", "name")

For $i = 1 To $arrContainers[0][0] - 1
    GUICtrlCreateTreeViewItem($arrContainers[$i][0], $Treeview)
Next
Opt("GUIOnEventMode", 1)
While 1
    $test = GUISetState()
    if $test = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
    
;Functions

Func SaveOU()
     ;RETURN THE CN NAME HERE
EndFunc   ;==>UpdateListLeft

Func OnExit()
    Exit
EndFunc
Link to comment
Share on other sites

Ive gotten this far but I cant seem to figure out what I need to finish. Ive got a script that will sniff out stale PCs in AD and delete them depending on what I set the password age to, that part works. The problem is the OU has to be hard coded so it's only good in this environment so I would like to add this custom search feature. I found an ADTool script (Author = myName?) in the forums that will open AD in a list view and I used this portion of it (attached). What I want to do is hit the save button and it pass the OU path on to the script that does the search in the CN=OU, DC=Domain, DC=Domain format. I have not done much/anything with GUI so I'm kinda treading new water here.

Try it with this, and see if you get the text you want as a first step:

Func SaveOU()
     Local $TV_Selected = GUICtrlRead($Treeview)
     Local $sSelected = ControlGetText($ADToolGUI, "", $TV_Selected)
     MsgBox(64, "Selected", "$sSelected = " & $sSelected)
EndFunc   ;==>UpdateListLeft

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for the help

If I add the advanced option, on $TV_Selected = GUICtrlRead($Treeview, 1)

I can get the display name of the OU in $TV_Selected, however, $sSelected does not have any results (it's blank), so, I get a result now, I just need to figure out how to get the distinguished name :)

Link to comment
Share on other sites

Ok, I've been playing around with this a bit.

If I display $arrContainers...... _ArrayDisplay($arrContainers).......there are 2 columns, Col1 is the display name that shows up in the treeview and Col2 is the Distinguished name and shows up as LDAP://OU=computers, DC=domain, DC=local (I have a winner)......, I can live with the Treeview showing col 2 and from there I can manipulate the data I need. So, how do I change or better yet, can someone explain how the treeview is outputting the display name instead of the the distinguished name

(Eg Col1- computers, user, domain accounts.....instead of Col2- LDAP://cn=computers, dc=domain, dc=local.)

(PS, did I mention GUI and multi-dimensional arrays are new water for me if you couldn't tell) I've only worked with shopping list type stuff.

Link to comment
Share on other sites

Ok, I figured out how to get the info I want, however I just created a new problem.

Here it is and it's probably an easy fix, I just don't know the GUI UDF yet.

I have my window up on the screen, I choose my container, hit save and 1 of 2 things happens, 1. nothing 2. or nothing. How to I break out of the loop without exiting the script?

$ADToolGUI = GUICreate("Active Directory Tool", 500, 680)
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

$Btn_SaveOU = GUICtrlCreateButton("<--- Search this OU", 325, 10, 160, 20)
GUICtrlSetOnEvent($Btn_SaveOU, "SaveOU")

$Treeview = GUICtrlCreateTreeView(10, 10, 300, 656, BitOR($GUI_SS_DEFAULT_TREEVIEW, $WS_BORDER), $WS_EX_CLIENTEDGE)
_ADGetObjectsInOU ($arrContainers, $strDNSDomain, "ObjectClass='organizationalUnit'", 1, "name,ADsPath", "name")

For $i = 1 To $arrContainers[0][0] - 1
    GUICtrlCreateTreeViewItem($arrContainers[$i][1], $Treeview)
Next
Opt("GUIOnEventMode", 1)
While 1
    $msg = GUISetState()
    if $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
    
;Functions for AD Search

Func SaveOU()    
 $TV_Selected = GUICtrlRead($Treeview, 1)
 $TV_Selected = StringTrimLeft($TV_Selected, 7)
MsgBox(64,"You selected",$TV_Selected)
Exit ; HOW DO I BREAK OUT OF THE LOOP, NOT THE SCRIPT??????
EndFunc

Func OnExit()
    Exit
EndFunc
Link to comment
Share on other sites

Ok, I figured out how to get the info I want, however I just created a new problem.

Here it is and it's probably an easy fix, I just don't know the GUI UDF yet.

I have my window up on the screen, I choose my container, hit save and 1 of 2 things happens, 1. nothing 2. or nothing. How to I break out of the loop without exiting the script?

Move this near the top of the script, at least before the GuiCreate: Opt("GUIOnEventMode", 1)

You already created the GUI before setting the mode, so it is probably in a GuiGetMsg mode instead.

:)

P.S. What technique did you use to get the FQDN of the OU?

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...