Jump to content

Active Directory _AD_GetObjectProperties


Recommended Posts

Func _AD()
    _AD_Open()
    If $strAccount = Null Then
        MsgBox(16, "NULL", "No user currently logged on." )
    ElseIf _AD_ObjectExists(StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2]) Then
    $aProperties = _AD_GetObjectProperties(StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2], "lastLogon,telephoneNumber")
    GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Last Logon Time : "& $aProperties[1][1]& @CRLF & _
    "Telephone number : "& $aProperties[2][1])
    _AD_Close()
    Else
    GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Account name " & StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & " not in AD")
    EndIf
EndFunc

Maybe someone could point me in the right direction in how to fix this issue. Just having issues with accounts that have empty fields in AD like for example telephoneNumber. "Telephone number : "& $aProperties[2][1]) since im pulling information and the information aint there the script crashes. Is it possible if that field is empty to ignore that line?

Link to comment
Share on other sites

What's the error you're receiving? I'm assuming that its the array or subscript out of range error?

If that is the case, Just nest the GUIctrlSetData in an If Statement:

if uBound($aProperties,1) >= 3 then
GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Last Logon Time : "& $aProperties[1][1]& @CRLF & _
    "Telephone number : "& $aProperties[2][1])
endif

Obviously you'll need to play around with the numbers to suit your script; But from what you've provided; The above should be skipped if $aProperties[2][$x] dimension is out of range, and therefore suppress the error.

EDIT:

Apologies, On hindsight I've realised you probably don't want to skip all the information, Just the telephone.... In that case:

if uBound($aProperties,1) >= 3 then
     GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Last Logon Time : "& $aProperties[1][1]& @CRLF & _
    "Telephone number : "& $aProperties[2][1])
else
     GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Last Logon Time : "& $aProperties[1][1])
endif
Edited by javiwhite

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Link to comment
Share on other sites

What is the return value of _AD_GetObjectProperties and what is the value of @error and @extended?

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

Func _AD()
    Local $strComputer = GUICtrlRead ($Input1)
    _AD_Open()
        If $strAccount = Null Then
    GUICtrlSetData($Output, "No user currently logged on to " & @CRLF & _
        $strComputer & ".")
       ElseIf _AD_ObjectExists(StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2]) Then
    $adObj_telephoneNumber = _AD_GetObjectProperties(StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2], 'telephoneNumber')
       If $adObj_telephoneNumber[0][0] <> 0 Then $adObj_telephoneNumber = $adObj_telephoneNumber[1][1]
    $adObj_LastLogon = _AD_GetObjectProperties(StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2], 'lastLogon')
       If $adObj_LastLogon[0][0] <> 0 Then $adObj_LastLogon = $adObj_LastLogon[1][1]
    GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Last Logon Time : "& $adObj_LastLogon & @CRLF & _
    "Telephone number : "& $adObj_telephoneNumber)
    _AD_Close()
      Else
    GUICtrlSetData($Output, "Username : "& StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & @CRLF & _
    "Account name " & StringSplit($strAccount, '\', $STR_ENTIRESPLIT)[2] & " does not exist in AD.")
    EndIf
EndFunc

I got it to work. Thanks guys for replying,

Link to comment
Share on other sites

:)

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

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