I am trying to export contact records from AD with the following script. I'm not much of a scripter and I am getting errors on Line:36, Char:4, Error:Expected statement, Code:800A0400, Source: MS VBScript compilation error. The whole script is pasted below, but line 36 is
& " from '" & LDAPQUERY & "' where objectClass= 'user' order by sn " _
Any help is greatly appreciated.
*******
Script*
*******
Listing 1: ExportContacts.vbs
'User parameters.
'You must change FILENAME and LDAPQUERY constants according to your
'requirements before using this script.
'Filename for exporting data from AD.
const FILENAME= "d:\contacts.csv"
'LDAP query to AD, where
'DC_server is the DC server name and
'domainname is your domain name.
const LDAPQUERY= "LDAP://DC_server/DC=domainname,DC=ru"
Dim con, com, rs, fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fspenTextFile(FILENAME, 2, True) ' ForReading = 1, ForWriting = 2, ForAppending = 8
'Create the .csv file header for
'Outlook 2000 and Outlook Express.
f.Write "Last Name,First Name,Email address" & VbCrLf
'Connect to AD.
Set con = CreateObject("ADODB.Connection")
Set com = CreateObject("ADODB.Command")
con.Provider = "ADsDSOObject"
con.Open "Active Directory Provider"
Set com.ActiveConnection = con
'This query selects only unhidden users and contacts.
com.CommandText = "select givenname, sn,objectCategory,objectClass,"_
& "mail,msExchHideFromAddressLists" _
& " from '" & LDAPQUERY & "' where objectClass= 'user' order by sn " _
& " or objectClass= 'contact' order by sn"
com.Properties("Page Size") = 1000
'Run query to AD.
Set rs = com.Execute
rs.MoveFirst
While Not rs.EOF
Tmail = rs.Fields("mail")
Tsn = rs.Fields("sn")
TgivenName = rs.Fields("givenName")
'Write to the .csv filethe records
'that don't have empty <Last Name>,
'<First Name>, or <Email Address> fields.
If ((Tmail <> "" And Tsn <> "" And _
TgivenName <> "") And IsNull(rs.Fields("msExchHideFromAddressLists"))) Then
f.Write Tsn & "," & TgivenName & "," & Tmail & VbCrLf
End If
rs.MoveNext
Wend
rs.Close
f.Close
wscript.quit
& " from '" & LDAPQUERY & "' where objectClass= 'user' order by sn " _
Any help is greatly appreciated.
*******
Script*
*******
Listing 1: ExportContacts.vbs
'User parameters.
'You must change FILENAME and LDAPQUERY constants according to your
'requirements before using this script.
'Filename for exporting data from AD.
const FILENAME= "d:\contacts.csv"
'LDAP query to AD, where
'DC_server is the DC server name and
'domainname is your domain name.
const LDAPQUERY= "LDAP://DC_server/DC=domainname,DC=ru"
Dim con, com, rs, fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fspenTextFile(FILENAME, 2, True) ' ForReading = 1, ForWriting = 2, ForAppending = 8
'Create the .csv file header for
'Outlook 2000 and Outlook Express.
f.Write "Last Name,First Name,Email address" & VbCrLf
'Connect to AD.
Set con = CreateObject("ADODB.Connection")
Set com = CreateObject("ADODB.Command")
con.Provider = "ADsDSOObject"
con.Open "Active Directory Provider"
Set com.ActiveConnection = con
'This query selects only unhidden users and contacts.
com.CommandText = "select givenname, sn,objectCategory,objectClass,"_
& "mail,msExchHideFromAddressLists" _
& " from '" & LDAPQUERY & "' where objectClass= 'user' order by sn " _
& " or objectClass= 'contact' order by sn"
com.Properties("Page Size") = 1000
'Run query to AD.
Set rs = com.Execute
rs.MoveFirst
While Not rs.EOF
Tmail = rs.Fields("mail")
Tsn = rs.Fields("sn")
TgivenName = rs.Fields("givenName")
'Write to the .csv filethe records
'that don't have empty <Last Name>,
'<First Name>, or <Email Address> fields.
If ((Tmail <> "" And Tsn <> "" And _
TgivenName <> "") And IsNull(rs.Fields("msExchHideFromAddressLists"))) Then
f.Write Tsn & "," & TgivenName & "," & Tmail & VbCrLf
End If
rs.MoveNext
Wend
rs.Close
f.Close
wscript.quit