rschaeferhig
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{*} )(& (&(!msExchHideFromAddressL ists=TRUE) (mailnickn ame=*))" & _
"(|(&(objectCategory=perso n)(objectC lass=user) (|(homeMDB =*)" & _
"(msExchHomeServerName=*)) ))));" & _
"extensionAttribute1,legac yExchangeD N,proxyAdd resses,msE xchHomeSer verName,ex tensionAtt ribute5;su btree"
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("extens ionAttribu te1")&","& oRS("legac yExchangeD N"))
'**// 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(" legacyExch angeDN")&" ,"&proxyAd dress
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
'**// 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{*}
"(|(&(objectCategory=perso
"(msExchHomeServerName=*))
"extensionAttribute1,legac
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("extens
'**// 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")
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
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\A llSMTPProx ies.txt"
'Setup output file
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile(filepa th, ForWriting, True)
'Create Objects for LDAP Queries
Set rootDSE = GetObject("LDAP://RootDSE" )
DomainContainer = rootDSE.Get("defaultNaming Context")
Set conn = CreateObject("ADODB.Connec tion")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
'LDAP query for all mail users
strLDAP = "<LDAP://" & DomainContainer & ">;(&(!cn=SystemMailbox{*} )(& (&(!msExchHideFromAddressL ists=TRUE) (mailnickn ame=*))(|( &(objectCa tegory=per son)(objec tClass=use r)(|(homeM DB=*)(msEx chHomeServ erName=*)) ))));adspa th;subtree "
'Get query results and output to file
Set oComm = CreateObject("ADODB.Comman d")
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.ext ensionAttr ibute1
legacyDN=FoundObject.legac yExchangeD N
test1=FoundObject.extensio nAttribute 5
exsvr=FoundObject.msExchHo meServerNa me
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=CONFIGU RATION/CN= SERVERS/CN =UK01EXCMB 02" 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(empl oyeeid& vbTab & legacydn & vbTab & Address)
End if
Next
on error goto 0
end if
rs.MoveNext
Wend
Const ForWriting = 2
'Path for the output file
filePath = "C:\Documents and Settings\rs02130\Desktop\A
'Setup output file
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile(filepa
'Create Objects for LDAP Queries
Set rootDSE = GetObject("LDAP://RootDSE"
DomainContainer = rootDSE.Get("defaultNaming
Set conn = CreateObject("ADODB.Connec
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
'LDAP query for all mail users
strLDAP = "<LDAP://" & DomainContainer & ">;(&(!cn=SystemMailbox{*}
'Get query results and output to file
Set oComm = CreateObject("ADODB.Comman
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.ext
legacyDN=FoundObject.legac
test1=FoundObject.extensio
exsvr=FoundObject.msExchHo
writeit=true
if mid(lcase(trim(test1)),1,5
writeit=false
end if
if Trim(UCase(exsvr)) = "/O=HIG/OU=UK01/CN=CONFIGU
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(empl
End if
Next
on error goto 0
end if
rs.MoveNext
Wend
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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{*}
"(|(&(objectCategory=perso
"(msExchHomeServerName=*))
"extensionAttribute1,legac
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("extens
'**// 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")
oWrite.WriteLine(sWrite)
Else
sWrite = "No proxy"
oWrite.WriteLine(sWrite)
End If
on error goto 0
oRS.MoveNext
Loop
'=============
Regards,
Rob.