locked
Utilizing ObjectClass to identify all objects in Active Directory that have a TargetAddresses attribute defined on them RRS feed

  • Question

  • I am working on an initiative to consolidate all the mailboxes in the environment and do some clean up. Purge all mailboxes that are no longer utilized and migrate those that are. I created a script to do what I believe will perform a lookup against Active Directory and identify all objects that have the TargetAddress attribute enabled on them regardless if they are mailboxes, computers, distro group or contacts. Here is the script utilized for this action. What results after attempting to run the script is an error: Typemismatch code:=800A000D. Line: 28 char:2

    I have identified the offending field as "objectClass";

    """" & rs.fields("objectClass") & """," & _

    It errors when refer to it like what is above or when written as such:

    """" & rs.fields(8).value & """," & _

    Therefore, the question is how do I refer to this field so I can obtain the proper value to identify the information being requested.

    Here is the script created for this process.

    
    '(&(&(&(objectCategory=person)(objectClass=user)(targetAddress=*@domain.onmicrosoft.com))))
    'http://www.windowsserverfaq.org/?url=/faq/ADQueries/LDAP-Queries.asp

    dim strToday, strTime
    strToday = MakeDate(now)
    strTime = MakeTime(now)


    Set fso = CreateObject("Scripting.FileSystemObject")
    set wfile = fso.opentextfile( strToday & strTime & "_anywithtargetAddress.csv",2,true)

     wfile.writeline("cn,name,legacyExchangeDN,targetAddress,distinguishedName,mailnickname,msExchHomeServerName,objectClass,objectCategory")
     set conn = createobject("ADODB.Connection")
     set com = createobject("ADODB.Command")
     Set iAdRootDSE = GetObject("LDAP://RootDSE")
     strNameingContext = iAdRootDSE.Get("configurationNamingContext")
     strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
     Conn.Provider = "ADsDSOObject"
     Conn.Open "ADs Provider"
     wscript.echo "strNameingContext=" & strNameingContext
     'svcQuery = "<LDAP://" & strNameingContext & ">;(&(objectCategory=msExchExchangeServer)(cn=" & Servername & "));cn,name,legacyExchangeDN,targetAddress;subtree"
     svcQuery = "<LDAP://" & strDefaultNamingContext & ">;(&(&(&(objectCategory=*)(objectClass=*)(targetAddress=*))));cn,name,legacyExchangeDN,targetAddress,distinguishedName,mailnickname,msExchHomeServerName,objectCategory,objectClass;subtree"
     Com.ActiveConnection = Conn
     Com.CommandText = svcQuery
     com.Properties("Page Size") = 2000
     Set Rs = Com.Execute
     while not rs.eof
     wfile.writeline( _
     """" & rs.fields("cn") & """," & _
     """" & rs.fields("name") & """," & _
     """" & rs.fields("legacyExchangeDN") & """," & _
     """" & rs.fields("targetAddress") & """," & _
     """" & rs.fields("distinguishedName") & """," & _
     """" & rs.fields("mailnickname") & """," & _
     """" & rs.fields("msExchHomeServerName") & """," & _
     """" & rs.fields("objectClass") & """," & _
     """" & rs.fields("objectCategory") & """" _
     )
     rs.movenext
     wend
     rs.close
     wfile.close
     set fso = nothing
     set conn = nothing
     set com = nothing
    wscript.echo "Finished with anywithtargetAddress_ldap.vbs"

    Function MakeDate(strDate)
         '
         ' This function returns an AS/400 friendly date string
         ' that follows the format of CCYY-MM-DD
         '
         Dim strYear
         Dim strMonth
         Dim strDay

         strYear = CStr(Year(strDate))
     
         strMonth = CStr(Month(strDate))

         If Len(strMonth) < 2 Then
              strMonth = "0" & strMonth
         End If

         strDay = CStr(Day(strDate))
     
         If Len(strDay) < 2 Then
              strDay = "0" & strDay
         End If


         strDate = strYear & "-" & strMonth & "-" & strDay
         MakeDate = strDate
    End Function

    Function MakeTime(strTime)
         '
         ' This function returns a time string in the
         ' following format: HHMMSS
          '
         Dim strHour
         Dim strMin
         Dim strSec

         strHour = CStr(Hour(strTime))
         If Len(strHour) < 2 Then
              strHour = "0" & strHour
         End If

         strMin = CStr(Minute(strTime))
          If Len(strMin) < 2 Then
              strMin = "0" & strMin
         End If

         strSec = CStr(Second(strTime))
         If Len(strSec) < 2 Then
              strSec = "0" & strSec
          End If

         strTime = strHour & strMin & strSec
         MakeTime = strTime
    End Function 


    Appreciate you, todonotes

    Tuesday, July 17, 2012 12:46 PM

Answers

  • On Tue, 17 Jul 2012 12:46:14 +0000, todonotes wrote:
     
    >
    >
    >I am working on an initiative to consolidate all the mailboxes in the environment and do some clean up. Purge all mailboxes that are no longer utilized and migrate those that are. I created a script to do what I believe will perform a lookup against Active Directory and identify all objects that have the TargetAddress attribute enabled on them regardless if they are mailboxes, computers, distro group or contacts. Here is the script utilized for this action. What results after attempting to run the script is an error: Typemismatch code:=800A000D. Line: 28 char:2
    >
    >I have identified the offending field as "objectClass";
     
    ObjectClass is a multi-valued property. You're probably interested in
    the LAST element -- that one's usually the most specific.
     
    You'll also have a problem with this:
    ==> com.Properties("Page Size") = 2000
     
    Reduce the 2000 to 1000.
     
    Your LDAP query is overly complex, too. All you really need is
    "(targetAddress=*)".
     
    If I were you I'd dump the VBS and use Powershell.
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    • Marked as answer by todonotes Friday, July 20, 2012 2:16 AM
    Wednesday, July 18, 2012 4:19 AM

All replies

  • Any reason why you dont want to just use ADUC, find, custom search, advanced, and enter the ldap query you have '(&(&(&(objectCategory=person)(objectClass=user)(targetAddress=*@domain.onmicrosoft.com))))

    ADUC will enumerate the results for you.


    James Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com

    Tuesday, July 17, 2012 1:44 PM
  • I felt this would be easier than running multiple queries against ADUC. But this is almost laughable with my question to the forum, right? :) But would still very much like to know the answer to my question posed regarding the script.


    Appreciate you, todonotes

    Tuesday, July 17, 2012 4:45 PM
  • Hi,
    If you have a W2K8, you can use Windows Powershell and get all objects with an targetaddress set with this oneliner:

    Get-ADObject -Filter * -Properties targetaddress | where {$_.targetaddress -notlike "$null"} | ft Name,Targetaddress,objectclass

    Just a little bit easier than your script :)
    The activedirectory module has to be imported first.

    But I must say that this is a bit off topic of this forum.

    Martina Miskovic

    Tuesday, July 17, 2012 8:15 PM
  • On Tue, 17 Jul 2012 12:46:14 +0000, todonotes wrote:
     
    >
    >
    >I am working on an initiative to consolidate all the mailboxes in the environment and do some clean up. Purge all mailboxes that are no longer utilized and migrate those that are. I created a script to do what I believe will perform a lookup against Active Directory and identify all objects that have the TargetAddress attribute enabled on them regardless if they are mailboxes, computers, distro group or contacts. Here is the script utilized for this action. What results after attempting to run the script is an error: Typemismatch code:=800A000D. Line: 28 char:2
    >
    >I have identified the offending field as "objectClass";
     
    ObjectClass is a multi-valued property. You're probably interested in
    the LAST element -- that one's usually the most specific.
     
    You'll also have a problem with this:
    ==> com.Properties("Page Size") = 2000
     
    Reduce the 2000 to 1000.
     
    Your LDAP query is overly complex, too. All you really need is
    "(targetAddress=*)".
     
    If I were you I'd dump the VBS and use Powershell.
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    • Marked as answer by todonotes Friday, July 20, 2012 2:16 AM
    Wednesday, July 18, 2012 4:19 AM
  • Not an option... Wish it were. Will give that a shot and post results.

    Appreciate you, todonotes

    Thursday, July 19, 2012 12:35 AM
  • After your info pointed out that it was an array, it was a piece of cake to compile. Will post script when I am in the office where the finished product is for the next person embarking on a crazy journey similar to mine!

    Thanks again!


    Appreciate you, todonotes

    Friday, July 20, 2012 2:17 AM
  • On Fri, 20 Jul 2012 02:17:42 +0000, todonotes wrote:
     
    >After your info pointed out that it was an array, it was a piece of cake to compile. Will post script when I am in the office where the finished product is for the next person embarking on a crazy journey similar to mine!
     
    Don't forget to lower the value you use for pagesize. If you want it
    to work with almost any domain controller then make the value not
    larger than 1000. If you want it to work with most Windows 2003+ DCs
    you can raise that to not more then 1500.
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    Friday, July 20, 2012 2:40 AM