Jump to content

Need a hint: Active Directory function


Recommended Posts

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.

Always carry a towel.

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

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 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

  • 2 weeks later...

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

Always carry a towel.

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...