Jump to content

Active Directory UDF - Looking for Testers of rewritten function _AD_ModifyAttribute


Recommended Posts

Hello all!

As I have just read access to my companies Active Directory I need some users willing to test the rewritten _AD_ModifyAttribute function.
My goal is to have the function handle single and multi value attributes the same way and support CLEAR, UPDATE, APPEND and DELETE for the attributes.

First step is to test how the function handles single value attributes:

Please modify the following script to specify the object (I suggest a dummy user in your test AD environment - the function might still be buggy).
Then please run the script and post the restults!

If everything works as expected we will test multi value attributes. AD attributes: http://www.rlmueller.net/UserAttributes.htm

#include <AD.au3>

_AD_Open()
$sObject = "user-to-modify" ; <== NEEDS TO BE CHANGED BY YOU!
$sAttribute = "Description"

; CLEAR - single value attribute
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
If @error Then Exit MsgBox(0, "Single value - Error!", "CLEAR: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "", 1)
If @error Then Exit MsgBox(0, "Single value - Error!", "CLEAR returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then Exit MsgBox(0, "Single value - Error!", "CLEAR: Query new value returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after CLEAR: " & $sReturnValue & @CRLF & "Expected value: ''")

; UPDATE - single value attribute
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
If @error Then Exit MsgBox(0, "Single value - Error!", "UPDATE: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "UPDATE", 2)
If @error Then Exit MsgBox(0, "Single value - Error!", "UPDATE returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then Exit MsgBox(0, "Single value - Error!", "UPDATE: Query new value returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after UPDATE: " & $sReturnValue & @CRLF & "Expected value: 'UPDATE'")

; APPEND - single value attribute - APPEND should work the same way as UPDATE
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
_AD_ModifyAttributeEX($sObject, $sAttribute, "APPEND", 3)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then Exit MsgBox(0, "Single value - Error!", "APPEND returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after APPEND: " & $sReturnValue & @CRLF & "Expected value: 'APPEND'")

; DELETE - single value attribute - DELETE should work the same way as CLEAR
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
_AD_ModifyAttributeEX($sObject, $sAttribute, "DELETE", 4)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then Exit MsgBox(0, "Single value - Error!", "DELETE returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after DELETE: " & $sReturnValue & @CRLF & "Expected value: ''")

_AD_Close()
Exit

; #FUNCTION# ====================================================================================================================
; Name...........: _AD_ModifyAttribute
; Description ...: Modifies an attribute of the given object to the value specified.
; Syntax.........: _AD_ModifyAttribute($sObject, $sAttribute[, $vValue = ""[, $iOption = 1]])
; Parameters ....: $sObject - Object (user, group ...) to add/delete/modify an attribute (sAMAccountName or FQDN)
;                  $sAttribute - Attribute to add/delete/modify
;                  $vValue - Optional: Value(s) to modify the attribute with. Use a blank string ("") to remove all values (default).
;                  +$vValue can be a single value (as a string) or a multi-value (as a zero-based one-dimensional array)
;                  $iOption - Optional: Indicates the mode of modification: Clear, Update, Append, Delete.
;                  |1 - CLEAR: remove all value(s) from the attribute (default when $vValue = "" or Default)
;                  |2 - UPDATE: replace the current value(s) with the specified value(s)
;                  |3 - APPEND: append the specified value(s) to the existing values(s)
;                  |4 - DELETE: delete the specified value(s) from the object
; Return values .: Success - 1
;                  Failure - 0, sets @error to:
;                  |1 - $sObject does not exist
;                  |2 - Parameter $iOption is invalid. needs to be in the range1 to 4.
;                  |x - Error returned by SetInfo method (Missing permission etc.)
; Author ........: Jonathan Clelland
; Modified.......: water
; Remarks .......:
; Related .......: _AD_GetObjectAttribute, _AD_GetObjectProperties, _AD_AddEmailAddress
; Link ..........: http://msdn.microsoft.com/en-us/library/aa746353(VS.85).aspx (ADS_PROPERTY_OPERATION_ENUM Enumeration)
; Example .......: Yes
; ===============================================================================================================================
Func _AD_ModifyAttributeEX($sObject, $sAttribute, $vValue = "", $iOption = 1)

    Local $aValue[1]
    If $vValue = Default Then $vValue = ""
    If IsArray($vValue) Then
        $aValue = $vValue
    Else
        ; Move the string value to the array
        $aValue[0] = $vValue
    EndIf
    If $iOption = Default Then $iOption = 1
    If $iOption < 1 Or $iOption > 4 Then Return SetError(2, 0, 0)
    If Not _AD_ObjectExists($sObject) Then Return SetError(1, 0, 0)
    Local $sProperty = "sAMAccountName"
    If StringMid($sObject, 3, 1) = "=" Then $sProperty = "distinguishedName" ; FQDN provided
    $__oAD_Command.CommandText = "<LDAP://" & $sAD_HostServer & "/" & $sAD_DNSDomain & ">;(" & $sProperty & "=" & $sObject & ");ADsPath;subtree"
    Local $oRecordSet = $__oAD_Command.Execute ; Retrieve the ADsPath for the object
    Local $sLDAPEntry = $oRecordSet.fields(0).Value
    Local $oObject = __AD_ObjGet($sLDAPEntry) ; Retrieve the COM Object for the object
    $oObject.GetInfo
    Switch $iOption
        Case 1
            $oObject.PutEx(1, $sAttribute, 0) ; CLEAR: remove all the property value(s) from the object
        Case 2
            $oObject.PutEx(2, $sAttribute, $aValue) ; UPDATE: replace the current value(s) with the specified value(s)
        Case 3
            $oObject.PutEx(3, $sAttribute, $aValue) ; APPEND: append the specified value(s) to the existing values(s)
        Case 4
            $oObject.PutEx(4, $sAttribute, $aValue) ; DELETE: delete the specified value(s) from the object
    EndSwitch
    $oObject.SetInfo
    If @error Then Return SetError(@error, 0, 0)
    Return 1

EndFunc   ;==>_AD_ModifyAttributeEX

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

So it seems that a property with no values gets deleted from the property cache. Looks good.

I will post a similar script for multi value properties quite soon :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

_AD_ModifyAttributeEX clears the description and it seems that a property without value gets removed from the property cache. That's why _AD_GetPbjectAttribute doesn't find the property in the cache and hence returns @error = 2.

Could you please run the modified version of the script so it doesn't exit on first error?

#include <AD.au3>

_AD_Open()
$sObject = "user-to-modify" ; <== NEEDS TO BE CHANGED BY YOU!
$sAttribute = "Description"

; CLEAR - single value attribute
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
If @error Then MsgBox(0, "Single value - Error!", "CLEAR: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "", 1)
If @error Then MsgBox(0, "Single value - Error!", "CLEAR returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Single value - Error!", "CLEAR: Query new value returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after CLEAR: " & $sReturnValue & @CRLF & "Expected value: ''")

; UPDATE - single value attribute
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
If @error Then MsgBox(0, "Single value - Error!", "UPDATE: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "UPDATE", 2)
If @error Then MsgBox(0, "Single value - Error!", "UPDATE returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Single value - Error!", "UPDATE: Query new value returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after UPDATE: " & $sReturnValue & @CRLF & "Expected value: 'UPDATE'")

; APPEND - single value attribute - APPEND should work the same way as UPDATE
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
If @error Then MsgBox(0, "Single value - Error!", "APPEND: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "APPEND", 3)
If @error Then MsgBox(0, "Single value - Error!", "APPEND returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Single value - Error!", "APPEND: Query new value returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after APPEND: " & $sReturnValue & @CRLF & "Expected value: 'APPEND'")

; DELETE - single value attribute - DELETE should work the same way as CLEAR
_AD_ModifyAttribute($sObject, $sAttribute, "Original value", 2)  ; Set the original value
If @error Then MsgBox(0, "Single value - Error!", "DELETE: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "DELETE", 4)
If @error Then MsgBox(0, "Single value - Error!", "DELETE returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Single value - Error!", "DELETE: Query new value returned @error = " & @error & ", @extended = " & @extended)
MsgBox(0, "Success!", "Value after DELETE: " & $sReturnValue & @CRLF & "Expected value: ''")

_AD_Close()
Exit

; #FUNCTION# ====================================================================================================================
; Name...........: _AD_ModifyAttribute
; Description ...: Modifies an attribute of the given object to the value specified.
; Syntax.........: _AD_ModifyAttribute($sObject, $sAttribute[, $vValue = ""[, $iOption = 1]])
; Parameters ....: $sObject - Object (user, group ...) to add/delete/modify an attribute (sAMAccountName or FQDN)
;                  $sAttribute - Attribute to add/delete/modify
;                  $vValue - Optional: Value(s) to modify the attribute with. Use a blank string ("") to remove all values (default).
;                  +$vValue can be a single value (as a string) or a multi-value (as a zero-based one-dimensional array)
;                  $iOption - Optional: Indicates the mode of modification: Clear, Update, Append, Delete.
;                  |1 - CLEAR: remove all value(s) from the attribute (default when $vValue = "" or Default)
;                  |2 - UPDATE: replace the current value(s) with the specified value(s)
;                  |3 - APPEND: append the specified value(s) to the existing values(s)
;                  |4 - DELETE: delete the specified value(s) from the object
; Return values .: Success - 1
;                  Failure - 0, sets @error to:
;                  |1 - $sObject does not exist
;                  |2 - Parameter $iOption is invalid. needs to be in the range1 to 4.
;                  |x - Error returned by SetInfo method (Missing permission etc.)
; Author ........: Jonathan Clelland
; Modified.......: water
; Remarks .......:
; Related .......: _AD_GetObjectAttribute, _AD_GetObjectProperties, _AD_AddEmailAddress
; Link ..........: http://msdn.microsoft.com/en-us/library/aa746353(VS.85).aspx (ADS_PROPERTY_OPERATION_ENUM Enumeration)
; Example .......: Yes
; ===============================================================================================================================
Func _AD_ModifyAttributeEX($sObject, $sAttribute, $vValue = "", $iOption = 1)

    Local $aValue[1]
    If $vValue = Default Then $vValue = ""
    If IsArray($vValue) Then
        $aValue = $vValue
    Else
        ; Move the string value to the array
        $aValue[0] = $vValue
    EndIf
    If $iOption = Default Then $iOption = 1
    If $iOption < 1 Or $iOption > 4 Then Return SetError(2, 0, 0)
    If Not _AD_ObjectExists($sObject) Then Return SetError(1, 0, 0)
    Local $sProperty = "sAMAccountName"
    If StringMid($sObject, 3, 1) = "=" Then $sProperty = "distinguishedName" ; FQDN provided
    $__oAD_Command.CommandText = "<LDAP://" & $sAD_HostServer & "/" & $sAD_DNSDomain & ">;(" & $sProperty & "=" & $sObject & ");ADsPath;subtree"
    Local $oRecordSet = $__oAD_Command.Execute ; Retrieve the ADsPath for the object
    Local $sLDAPEntry = $oRecordSet.fields(0).Value
    Local $oObject = __AD_ObjGet($sLDAPEntry) ; Retrieve the COM Object for the object
    $oObject.GetInfo
    Switch $iOption
        Case 1
            $oObject.PutEx(1, $sAttribute, 0) ; CLEAR: remove all the property value(s) from the object
        Case 2
            $oObject.PutEx(2, $sAttribute, $aValue) ; UPDATE: replace the current value(s) with the specified value(s)
        Case 3
            $oObject.PutEx(3, $sAttribute, $aValue) ; APPEND: append the specified value(s) to the existing values(s)
        Case 4
            $oObject.PutEx(4, $sAttribute, $aValue) ; DELETE: delete the specified value(s) from the object
    EndSwitch
    $oObject.SetInfo
    If @error Then Return SetError(@error, 0, 0)
    Return 1

EndFunc   ;==>_AD_ModifyAttributeEX

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

Thanks for your reply!

The next version will only support CLEAR and UPDATE for single value attributes. Seems APPEND and DELETE do not make much sense in this case.

Will soon post some examples to test multi value attributes :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

Test version for multi value attributes. Hope there are not too many bugs ;)

#include <AD.au3>

; The ADS_PROPERTY_OPERATION_ENUM enumeration specifies ways to update a named property in the cache
; https://docs.microsoft.com/en-us/windows/desktop/api/iads/ne-iads-__midl___midl_itf_ads_0000_0000_0027
Global Const $ADS_PROPERTY_CLEAR = 1
Global Const $ADS_PROPERTY_UPDATE = 2
Global Const $ADS_PROPERTY_APPEND = 3
Global Const $ADS_PROPERTY_DELETE = 4

_AD_Open()
Global $sObject = "user-to-modify" ; <== NEEDS TO BE CHANGED BY YOU!

Global $sAttribute = "postalAddress"
Global $aAttributeValues[] = ["address 1", "address 2", "address 3", "address 4"]
Global $aNewValues[] = ["address 5", "address 6"]
Global $aDeleteValues[] = ["address 2", "address 5"]

; CLEAR - Multi value attribute
_AD_ModifyAttribute($sObject, $sAttribute, $aAttributeValues, 2) ; Set the original value
If @error Then MsgBox(0, "Multi value - Error!", "CLEAR: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, "", $ADS_PROPERTY_CLEAR)
If @error Then MsgBox(0, "Multi value - Error!", "CLEAR returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Multi value - Error!", "CLEAR: Query new value returned @error = " & @error & ", @extended = " & @extended)
If IsString($sReturnValue) Then
    MsgBox(0, "Success!", "Value after CLEAR: " & $sReturnValue & @CRLF & "Expected value: ''")
Else
    _ArrayDisplay($sReturnValue, "Value after CLEAR. Should be ''")
EndIf

; UPDATE - Multi value attribute
_AD_ModifyAttribute($sObject, $sAttribute, $aAttributeValues, 2) ; Set the original value
If @error Then MsgBox(0, "Multi value - Error!", "UPDATE: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, $aNewValues, $ADS_PROPERTY_UPDATE)
If @error Then MsgBox(0, "Multi value - Error!", "UPDATE returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Multi value - Error!", "UPDATE: Query new value returned @error = " & @error & ", @extended = " & @extended)
If IsString($sReturnValue) Then
    MsgBox(0, "Success!", "Value after UPDATE: " & $sReturnValue & @CRLF & "Expected value: ''")
Else
    _ArrayDisplay($sReturnValue, "Value after UPDATE should be address 5 and address 6")
EndIf

; APPEND - Multi value attribute
_AD_ModifyAttribute($sObject, $sAttribute, $aAttributeValues, 2) ; Set the original value
If @error Then MsgBox(0, "Multi value - Error!", "APPEND: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, $aNewValues, 3)
If @error Then MsgBox(0, "Multi value - Error!", "APPEND returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Multi value - Error!", "APPEND: Query new value returned @error = " & @error & ", @extended = " & @extended)
If IsString($sReturnValue) Then
    MsgBox(0, "Success!", "Value after APPEND: " & $sReturnValue & @CRLF & "Expected value: ''")
Else
    _ArrayDisplay($sReturnValue, "Value after APPEND should be address 1 to 6")
EndIf

; DELETE - Multi value attribute
_AD_ModifyAttribute($sObject, $sAttribute, $aAttributeValues, 2) ; Set the original value
If @error Then MsgBox(0, "Multi value - Error!", "DELETE: Set original value returned @error = " & @error & ", @extended = " & @extended)
_AD_ModifyAttributeEX($sObject, $sAttribute, $aDeleteValues, 4)
If @error Then MsgBox(0, "Multi value - Error!", "DELETE returned @error = " & @error & ", @extended = " & @extended)
$sReturnValue = _AD_GetObjectAttribute($sObject, $sAttribute)
If @error Then MsgBox(0, "Multi value - Error!", "DELETE: Query new value returned @error = " & @error & ", @extended = " & @extended)
If IsString($sReturnValue) Then
    MsgBox(0, "Success!", "Value after DELETE: " & $sReturnValue & @CRLF & "Expected value: ''")
Else
    _ArrayDisplay($sReturnValue, "Value after DELETE should be address 1, 3, 4, 6")
EndIf

_AD_Close()
Exit

; #FUNCTION# ====================================================================================================================
; Name...........: _AD_ModifyAttribute
; Description ...: Modifies an attribute of the given object to the value specified.
; Syntax.........: _AD_ModifyAttribute($sObject, $sAttribute[, $vValue = ""[, $iOption = 1]])
; Parameters ....: $sObject - Object (user, group ...) to add/delete/modify an attribute (sAMAccountName or FQDN)
;                  $sAttribute - Attribute to add/delete/modify
;                  $vValue - Optional: Value(s) to modify the attribute with. Use a blank string ("") to remove all values (default).
;                  +$vValue can be a string for a single value attribute (e.g. description) or
;                  +a zero-based one-dimensional array for a multi value attribute (e.g. PostalAdresses).
;                  $iOption - Optional: Indicates the mode of modification: Clear, Update, Append, Delete.
;                  |1 - CLEAR: remove all value(s) from the attribute (default when $vValue = "" or Default)
;                  |2 - UPDATE: replace the current value(s) with the specified value(s)
;                  |3 - APPEND: append the specified value(s) to the existing values(s)
;                  |4 - DELETE: delete the specified value(s) from the object
; Return values .: Success - 1
;                  Failure - 0, sets @error to:
;                  |1 - $sObject does not exist
;                  |2 - Parameter $iOption is invalid. Needs to be in the range1 to 4.
;                  |3 - Parameter $iOption is invalid for a single value attribute. Needs to be 1 or 2.
;                  |x - Error returned by SetInfo method (Missing permission etc.)
; Author ........: Jonathan Clelland
; Modified.......: water
; Remarks .......: For a single value attribute the function only supports $iOption 1 and 2.
; Related .......: _AD_GetObjectAttribute, _AD_GetObjectProperties, _AD_AddEmailAddress
; Link ..........: https://support.microsoft.com/en-us/help/260251/how-to-use-adsi-to-set-ldap-directory-attributes
; Example .......: Yes
; ===============================================================================================================================
Func _AD_ModifyAttributeEX($sObject, $sAttribute, $vValue = "", $iOption = 1)

    Local $aValue[1]
    If $vValue = Default Then $vValue = ""
    If $iOption = Default Then $iOption = 1
    If $iOption < 1 Or $iOption > 4 Then Return SetError(2, 0, 0)
    If IsArray($vValue) Then
        $aValue = $vValue
    Else ; Move the string value to the array
        If $iOption > 2 Then SetError(3, 0, 0)
        $aValue[0] = $vValue
    EndIf
    If Not _AD_ObjectExists($sObject) Then Return SetError(1, 0, 0)
    Local $sProperty = "sAMAccountName"
    If StringMid($sObject, 3, 1) = "=" Then $sProperty = "distinguishedName" ; FQDN provided
    $__oAD_Command.CommandText = "<LDAP://" & $sAD_HostServer & "/" & $sAD_DNSDomain & ">;(" & $sProperty & "=" & $sObject & ");ADsPath;subtree"
    Local $oRecordSet = $__oAD_Command.Execute ; Retrieve the ADsPath for the object
    Local $sLDAPEntry = $oRecordSet.fields(0).Value
    Local $oObject = __AD_ObjGet($sLDAPEntry) ; Retrieve the COM Object for the object
    $oObject.GetInfo
    Switch $iOption
        Case 1
            $oObject.PutEx($ADS_PROPERTY_CLEAR, $sAttribute, 0) ; CLEAR: remove all the property value(s) from the object
        Case 2
            $oObject.PutEx($ADS_PROPERTY_UPDATE, $sAttribute, $aValue) ; UPDATE: replace the current value(s) with the specified value(s)
        Case 3
            $oObject.PutEx($ADS_PROPERTY_APPEND, $sAttribute, $aValue) ; APPEND: append the specified value(s) to the existing values(s)
        Case 4
            $oObject.PutEx($ADS_PROPERTY_DELETE, $sAttribute, $aValue) ; DELETE: delete the specified value(s) from the object
    EndSwitch
    $oObject.SetInfo
    If @error Then Return SetError(@error, 0, 0)
    Return 1

EndFunc   ;==>_AD_ModifyAttributeEX

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

Seems we need to do some investigation why _AD_ModifyAttribute does not set the starting value.
Will come back with some debugging code.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

Think I used the wrong name for the attribute. Should be "postalAddress" not "PostalAddresses".
I modified my script above.
Could you please retry?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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...