Sign in to follow this  
Followers 0
ModemJunki

Need a hint: Active Directory function

4 posts in this topic

I'm pretty clueless about this, so I need to ask for a hint.

I need to update the "Managed By" field for a bunch of machines in a particular OU in AD.

Lets assume I can look through the superb AD functions posted in the example scripts and figure out how to query my OU, build array list of machine names, etc.. (big assumption but I may surprise myself).

What statement would update the "Managed By" field? In other words, what is the object name of the managed by field and how do I write to it?

Always noob questions from me, board needs a dunce cap smiley.

Share this post


Link to post
Share on other sites



$sManagerDN = "cn=YourUserName,ou=Test,dc=YourDomain,dc=local"
$oMachine.Put("ManagedBy", $sManagerDN)
$oMachine.SetInfo

So you need the obj reference to the machine (or machines, in a loop) in $oMachine, and you need the distinguished name of the manager to set in $sManagerDN.

:graduated:


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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Using the AD UDF you could write something like:

$iResult = _AD_ModifyAttribute($sComputer, "ManagedBy", $sManagerDN)

Be sure to specify a dollar sign as suffix to $sComputer if you specify the SamAccountName e.g.

$iResult = _AD_ModifyAttribute(@ComputerName & "$", "ManagedBy", $sManagerDN)
if you want to modify your computer. Edited by water

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!

Here is what I ended up with. The target is a bit odd - we don't want the end user name in there, instead a distribution list, all of the machines in the particular OU end up with the same "managed by" attribute.

Seems to work just fine for me!

#AutoIt3Wrapper_Au3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Au3Check_Stop_OnWarning=Y
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.0.0

    Sets managed by to a fixed string.

    Requires ad.au3 UDFs (download from AutoIT forums)

#ce ----------------------------------------------------------------------------
#include <AD.au3>

; *****************************************************************************
; Here we define our strings. Change these as needed.
; *****************************************************************************
;~ Base OU
Global $sOU = "OU=HERE,OU=CHANGE,OU=THIS,OU=TO,OU=MEET,OU=YOUR,OU=NEEDs,DC=YOUR_TLDC,DC=YOUR_TLDC,DC=YOUR_TLDC"
;~ Machine name prefilter (if you need it)
Global $PreFilter = "xxx"
;~ ManagedBy object string as a string
Global $sMgdByString = "CN=Auto IT,OU=THE-OU,OU=THE-OU,OU=THE-OU,OU=THE-OU,OU=THE-OU,DC=YOUR_TLDC,DC=YOUR_TLDC,DC=YOUR_TLDC"

; *****************************************************************************
; THE CODE BELOW SHOULD NEED NO CHANGES FOR MAINTENANCE
; *****************************************************************************
Global $aObjects[1][1][1]
Global $sCompName = ""
Global $iResult, $sCurrMgdBy

; Open Connection to the Active Directory
_AD_Open()

; *****************************************************************************
; Updates the managed by field to our defined string.
; *****************************************************************************
$aObjects = _AD_GetObjectsInOU($sOU, "(cn=" & $PreFilter & "*)", 2, "CN,managedBy,sAMAccountName")
If @error > 0 Then
    MsgBox(64, "Error", "Incorrect OU specified?")
Else
    _ArrayDelete($aObjects, 0)
EndIf

For $i = 0 To UBound($aObjects) - 1
    $sCurrMgdBy = $aObjects[$i][1]
    If $sCurrMgdBy <> $sMgdByString Then
        $sCompName = $aObjects[$i][2]
        _AD_ModifyAttribute($sCompName, "ManagedBy", "", 4)
        _AD_ModifyAttribute($sCompName, "ManagedBy", $sMgdByString, 2)
    EndIf
Next

; Close Connection to the Active Directory
_AD_Close()

; **********************************************************
; Executes LDAP queries and displays the results in an Array
; **********************************************************
Func _Examples($query, $fields, $description)

    Local $aObjects[1][1]
    $aObjects = _AD_GetObjectsInOU($sOU, $query, 2, $fields)
    If @error <> 0 Then
        MsgBox(64, "Active Directory Functions - Extended Example", "No entries found for LDAP query " & @CRLF & $query & @CRLF & $description & @CRLF & "Error: " & @error)
    Else
        _ArrayDisplay($aObjects, "LDAP query - " & $description & " - " & $query)
    EndIf

EndFunc   ;==>_Examples

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
Sign in to follow this  
Followers 0