VBScript错误80040E14 FROM子句中的语法错误

时间:2011-05-11 17:05:59

标签: csv vbscript active-directory

我正在尝试使用我在互联网上找到的脚本,允许使用VBScript和CSV文件在Active Directory中批量创建新用户帐户。我没有使用CSVDE b / c这个脚本也会创建密码。运行代码时我一直遇到这个错误我无法理解。有人可以帮忙吗?

'*********************************************************************
' Script: createUsersFromCSV.vbs                                     *
' Creates new user accounts in Active Directory from a CSV file.     *
' Input: CSV file with layout logonname,firstname,lastname,password  *
'                                                                    *
'*********************************************************************

Option Explicit

Dim sCSVFileLocation
Dim sCSVFile
Dim oConnection
Dim oRecordSet
Dim oNewUser

' Variables needed for LDAP connection
Dim oRootLDAP
Dim oContainer

' Holding variables for information import from CSV file
Dim sLogon
Dim sFirstName
Dim sLastName
Dim sDisplayName
Dim sPassword
Dim nPwdLastSet
Dim nUserAccountControl ' Used to enable the account
Dim sDomain
Dim sCompany
Dim sPhone
Dim sEmail
Dim sDescription

Dim NumChar, Count, strRdm, intRdm
Dim fso, f, fso1, f1

'* Modify this to match your company's AD domain
sDomain="mydomain.local"

'* Input file location
sCSVFileLocation = "C:\Documents and Settings\Administrator\Desktop\" 'KEEP TRAILING SLASH!

'* Full path to input file
sCSVFile = sCSVFileLocation&"newusers.csv"

' Commands used to open the CSV file and select all of the records
set oConnection = createobject("adodb.connection")
set oRecordSet = createobject("adodb.recordset")
oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & _
sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited"""
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection

' Create a connection to an Active Directory OU container.
Set oRootLDAP = GetObject("LDAP://rootDSE")
Set oContainer = GetObject("LDAP://ou=Test," & _
oRootLDAP.Get("defaultNamingContext")) 

on error resume next 

do until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file.



   ' --------- Start creating user account
   ' Read variable information from the CSV file
   ' and build everything needed to create the account
   sLogon = oRecordSet.Fields.Item(0).value
   sFirstName = oRecordSet.Fields.Item(1).value
   sLastName = oRecordSet.Fields.Item(2).value
   sDisplayName = sFirstName&" "&sLastName
   sPassword = oRecordSet.Fields.Item(3).value

   ' Build the User account
Set oNewUser = oContainer.Create("User","cn="&sFirstName&" "&sLastName)
oNewUser.put "sAMAccountName",lcase(sLogon)
oNewUser.put "givenName",sFirstName
oNewUser.put "sn",sLastName
oNewUser.put "UserPrincipalName",lcase(SLogon)&"@"&sDomain
oNewUser.put "DisplayName",sDisplayName
oNewUser.put "name",lcase(sLogon)

' Write this information into Active Directory so we can
' modify the password and enable the user account
oNewUser.SetInfo

' Change the users password
oNewUser.SetPassword sPassword
oNewUser.Put "pwdLastSet", 0

' Enable the user account
oNewUser.Put "userAccountControl", 512
oNewUser.SetInfo


objFile.Close

'*******************
oRecordset.MoveNext
Loop
'*******************
' Used only for debugging
'if err.number = -2147019886 then
' msgbox "User logon " & sLogon & "already exists"
'End If
' --------- End of user account creation

这是发生错误的地方,第51行char 1:

oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection

1 个答案:

答案 0 :(得分:0)

也许sCSVFile包含特殊字符,因此必须像这样转义:

oRecordSet.open "SELECT * FROM [" & sCSVFile & "]", oConnection 

我希望它有所帮助。