Link to home
Start Free TrialLog in
Avatar of rschaeferhig
rschaeferhigFlag for United States of America

asked on

WMI Script to get AD proxyAddresses returns nothing

I'm trying to get a couple of fields out of AD for each user, plus all their SMTP addresses out of proxyAddresses. I can get the single-value fields I want, but no proxyAddresses are being written out. Here's the relevant segments of my script:

'**// Establish the ADO connection to the directory
oConnection.open "Provider=ADsDSOObject;"
oCommand.ActiveConnection = oConnection

'**// Return the ADsPath for the forest
set oGC = GetObject("GC:")
for each oChild in oGC
      sADsPath = oChild.ADsPath      
next

'**// Establish ADODB.Command properties - Specify query to execute against the global catalog
oCommand.CommandText = _
      "<" & sADsPath & ">;(&(!cn=SystemMailbox{*})(& (&(!msExchHideFromAddressLists=TRUE)(mailnickname=*))" & _
      "(|(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)" & _
      "(msExchHomeServerName=*))))));" & _
      "extensionAttribute1,legacyExchangeDN,proxyAddresses,msExchHomeServerName,extensionAttribute5;subtree"
oCommand.Properties("Page Size") = 1000

'**// Populate ADODB.RecordSet with the results of the global catalog query
set oRS = oCommand.execute

'**// Verify that the RecordSet is not empty and iterate through the records
if oRS.RecordCount > 0 then
      oRS.MoveFirst
      do until oRS.EOF
        oLog.WriteLine(oRS("extensionAttribute1")&","&oRS("legacyExchangeDN"))
        '**// Iterate through the proxyAddresses collection.  If no proxy addresses exist, an error is
        '**// generated stating that the object is not a collection.  If error is raised, ignore and proceed
        on error resume next
        for each proxyAddress in oRS("proxyAddresses")
          '**//  write data to the output file
          sWrite = oRS("extensionAttribute1")&","&oRS("legacyExchangeDN")&","&proxyAddress
          oWrite.WriteLine(sWrite)
        next
      on error goto 0
      oRS.MoveNext
                     Loop

The oLog.Writelines are dumping out the two attributes I need, but the loop to process the proxyAddresses apparently never executes. If I put oLog.Writeline inside the loop nothing is written to the log file.

Any ideas
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, try this in place of the code you posted:
I have checked whether proxyAddresses is an array before going through it.
Also, if you still get unexpected results, I would comment out the On Error Resume Next statement so that you know what and where the error is....

'=============
**// Establish the ADO connection to the directory
oConnection.open "Provider=ADsDSOObject;"
oCommand.ActiveConnection = oConnection

'**// Return the ADsPath for the forest
set oGC = GetObject("GC:")
for each oChild in oGC
      sADsPath = oChild.ADsPath      
next

'**// Establish ADODB.Command properties - Specify query to execute against the global catalog
oCommand.CommandText = _
      "<" & sADsPath & ">;(&(!cn=SystemMailbox{*})(& (&(!msExchHideFromAddressLists=TRUE)(mailnickname=*))" & _
      "(|(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)" & _
      "(msExchHomeServerName=*))))));" & _
      "extensionAttribute1,legacyExchangeDN,proxyAddresses,msExchHomeServerName,extensionAttribute5;subtree"
oCommand.Properties("Page Size") = 1000

'**// Populate ADODB.RecordSet with the results of the global catalog query
set oRS = oCommand.execute

'**// Verify that the RecordSet is not empty and iterate through the records
if oRS.RecordCount > 0 then
      oRS.MoveFirst
      do until oRS.EOF
        oLog.WriteLine(oRS("extensionAttribute1")&","&oRS("legacyExchangeDN"))
        '**// Iterate through the proxyAddresses collection.  If no proxy addresses exist, an error is
        '**// generated stating that the object is not a collection.  If error is raised, ignore and proceed
        on error resume Next
        arrProxyAddrs = oRS("proxyAddresses")
            strProxyAddrs = "" 
            ' Check if any values returned.
            If IsArray(arrProxyAddrs) Then
                  For Each strAddr In arrProxyAddrs
                        If strProxyAddrs = "" Then
                              strProxyAddrs = strAddr
                        Else
                              strProxyAddrs = strProxyAddrs & ";" & strAddr
                        End If
                  Next
                  sWrite = oRS("extensionAttribute1")&","&oRS("legacyExchangeDN")&","&strProxyAddrs
                  oWrite.WriteLine(sWrite)
            Else
                  sWrite = "No proxy"
                  oWrite.WriteLine(sWrite)
            End If
       on error goto 0
      oRS.MoveNext
      Loop
'=============

Regards,

Rob.
Avatar of rschaeferhig

ASKER

THanks. I found the following on the web and tweaked it for my purposes. It works. It doesn't handle the proxyAddresses potential error, but if there are no proxyAddresses I don't want a record anyway.

Const ForWriting = 2

'Path for the output file
filePath = "C:\Documents and Settings\rs02130\Desktop\AllSMTPProxies.txt"

'Setup output file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(filepath, ForWriting, True)

'Create Objects for LDAP Queries
Set rootDSE = GetObject("LDAP://RootDSE")
DomainContainer = rootDSE.Get("defaultNamingContext")

Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"

'LDAP query for all mail users
strLDAP = "<LDAP://" & DomainContainer & ">;(&(!cn=SystemMailbox{*})(& (&(!msExchHideFromAddressLists=TRUE)(mailnickname=*))(|(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))))));adspath;subtree"

'Get query results and output to file
Set oComm = CreateObject("ADODB.Command")
oComm.ActiveConnection = conn
oComm.CommandText = strLDAP
oComm.Properties("Page size") = 3000

Set rs = oComm.Execute

While Not rs.EOF
                     on error resume next
      Set FoundObject = GetObject (rs.Fields(0).Value)
      employeeid=FoundObject.extensionAttribute1
      legacyDN=FoundObject.legacyExchangeDN
      test1=FoundObject.extensionAttribute5
      exsvr=FoundObject.msExchHomeServerName
      writeit=true
      if mid(lcase(trim(test1)),1,5)="dfx:1" then
            writeit=false
      end if
      if Trim(UCase(exsvr)) = "/O=HIG/OU=UK01/CN=CONFIGURATION/CN=SERVERS/CN=UK01EXCMB02" then
            writeit=false
      end if
      select case trim(lcase(employeeid))
            case "n/a"
                  writeit = false
            case "retired"
                  writeit = false
            case "termed"
                  writeit = false
            case "contractor"
                  writeit = false
            case "vendor"
                                 writeit = false
      end select
      if len(employeeid)<6 then
            writeit=false
      end if
      if writeit=true then
            arrProxyAddresses = FoundObject.proxyAddresses
            For Each Address In arrProxyAddresses
                If lcase(left(Address,5))= "smtp:" Then
                objTextFile.writeLine(employeeid& vbTab & legacydn & vbTab & Address)
              End if
            Next
            on error goto 0
      end if
    rs.MoveNext
Wend
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial