Script AutoIt to store MAC address in Active Directory



  • Hello,
    I'v made a script to store MAC address in computer account for Active Directory:

    #include <AD.au3>
    
    ;Enter here a username of an account that can change users in AD
    $utilisateur=""
    ;Enter here the user password
    $motdepasse=""
    ;AD Connexion
    _AD_Open($utilisateur,$motdepasse)
    ;Call function that put MAC in attribut "networkAddress" of machine account
    _AD_ModifyAttributeEX(@computername & "$","networkAddress",GET_MAC(@IPAddress1),2)
    _AD_Close()
    
    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
    
    Func GET_MAC($_MACsIP)
        Local $_MAC,$_MACSize
        Local $_MACi,$_MACs,$_MACr,$_MACiIP
        $_MAC = DllStructCreate("byte[6]")
        $_MACSize = DllStructCreate("int")
        DllStructSetData($_MACSize,1,6)
        $_MACr = DllCall ("Ws2_32.dll", "int", "inet_addr", "str", $_MACsIP)
        $_MACiIP = $_MACr[0]
        $_MACr = DllCall ("iphlpapi.dll", "int", "SendARP", "int", $_MACiIP, "int", 0, "ptr", DllStructGetPtr($_MAC), "ptr", DllStructGetPtr($_MACSize))
        $_MACs  = ""
        For $_MACi = 0 To 5
        If $_MACi Then $_MACs = $_MACs & ":"
            $_MACs = $_MACs & Hex(DllStructGetData($_MAC,1,$_MACi+1),2)
        Next
        DllClose($_MAC)
        DllClose($_MACSize)
        Return $_MACs
    EndFunc
    

    Put the code in AutoIt, set the user and password to connect to your active directory, save and convert to exe to use it via GPO.
    Then in Veyon Master set "LDAP Basic" -> "Environment settings" -> "Computer MAC address attribute" enter "networkAddress"


Log in to reply