Results 1 to 6 of 6

Thread: Searching Active Directory Sub OU's with script

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    3

    Searching Active Directory Sub OU's with script

    I have an old vbs script that I'd like to reuse versus rewrite. It works, but it's not able to search sub OU's using LDAP search.
    It does use a csv as a validation script file, and that part works fine.
    I have a feeling this is an easy fix to make it search resursively. Any assistance would be much appreciated.
    Without giving too much private information away:

    Code:
    Sub DumpLDAPUserInfo
    Dim MyiOUcnt
    Dim MyioUsercnt
    Dim MyarrCols
    Dim MysKey
    Dim MysEmail
    ' Insert code securely
        sLDAPServer = "test.com"
        sUser = "testuser"
        sPassword = "Password1"
        sCN = "/CN=" & sUser 
        sDN = "CN=" & sUser & "OU=Users,OU=Corporation,DC=Test,DC=com"
        sOUDNQuery = "LDAP://test.com/OU=Users,OU=Corporation,DC=Test,DC=com"
    
        Set oDS = GetObject("LDAP:")
        Set oOUDN = oDS.OpenDSObject( _
                    sOUDNQuery, _
                    sUser, _
                    sPassword, _
                    ADS_SECURE_AUTHENTICATION + ADS_SERVER_BIND)
        sMsg = oOUDN.Class & vbCrLf & sOUDNQuery & vbCrLf & _
               "Click OK to Continue ..." 
        Call DisplayMsg(sMsg,5,vbInformation)
        MyiOUcnt = 0
        MyioUsercnt = 0
        sLastCN = ""
        For each oOU in oOUDN
            MyiOUcnt = MyiOUcnt + 1
            MysKey = oOU.cn
            If dictValidation.Exists(MysKey) Then
               MyarrCols = Split(dictValidation(MysKey),"|")
               sEmplID = MysKey
               sFirstName = MyarrCols(10)
               sLastName = MyarrCols(11)
               sEmail = MyarrCols(9)
               MysEmail = oOU.mail
               If (oOU.mail <> "") AND (lcase(oOU.mail) <> lcase(sEmail)) Then
    '              If MyioUsercnt < 10 Then 
    '                 sMsg = oOU.cn & vbCrLf & _
    '                   oOU.displayName  & vbCrLf & _
    '                   oOU.givenName  & vbCrLf & _
    '                   oOU.sn & vbCrLf & _
    '                   oOU.mail & vbCrLf & _
    '                   "Old email: " & sEmail
    '                 Call DisplayMsg(sMsg,2,vbInformation)
    '              End If
                  Call WriteOutput
                  MyioUsercnt = MyioUsercnt + 1
               End If
               sLastCN = oOU.cn
            End If
        Next 
        sMsg = "oOU count=" & CStr(MyiOUcnt) & vbCrLf & _
               "oUser count=" & Cstr(MyioUsercnt) & vbCrLf & _
               "Output count=" & Cstr(iOutcnt)
    End Sub
    Last edited by jscharfenberg; Apr 1st, 2021 at 01:13 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Searching Active Directory Sub OU's with script

    Hi jscharfenberg, welcome to the forum!

    Please use code tags (the "#" button in the toolbar above the message you're about to post). Also, use "Option Explicit " if you're not doing so already and format your code so it can easily be read by others. And I did a quick Google search, there appear to enough sites dealing with topic.

    yours,
    Peter Swinkels

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    3

    Re: Searching Active Directory Sub OU's with script

    trying to delete reply...won't let me
    Last edited by jscharfenberg; Apr 1st, 2021 at 01:15 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    3

    Re: Searching Active Directory Sub OU's with script

    I searched for days myself, and never having done anything with vbscript I'm unsure how to incorporate the recursive search into the script. Also if the findings were even valid or relevant and they all were marked from 2011 and prior. Any assistance would be helpful. For someone who knows vbscript, I bet its simple. For someone who doesn't (me), I'm at a loss.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Searching Active Directory Sub OU's with script

    Alright, while I can help with vbscript in general I am afraid I can't answer your specific question. Sorry.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Searching Active Directory Sub OU's with script

    https://devblogs.microsoft.com/scrip...-ous-in-an-ou/

    https://stackoverflow.com/questions/...for-nested-ous

    https://www.tek-tips.com/viewthread.cfm?qid=972552


    Querying AD via VBScript has been around for many, many years. So a post from 2004 about the subject shouldn't be dismissed out of hand.

    If your goal is for someone to simply provide working code for you to copy/paste directly into your solution, I'm unable to provide such assistance.

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width