【问题标题】:VBScript Error 80040E14 Syntax error in FROM clauseVBScript 错误 80040E14 FROM 子句中的语法错误
【发布时间】:2011-05-11 17:05:58
【问题描述】:

我正在尝试使用我在 Internet 上找到的脚本来允许使用 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 行字符 1:

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

【问题讨论】:

  • 您需要选择FROM 一个表名,而不是一个文件路径。
  • 它从 CSV 文件中提取。 @fmunkert 想出了一个解决方法,可以帮助我确定我正在运行的文件夹正在搞砸事情。还是谢谢。

标签: csv vbscript active-directory


【解决方案1】:

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

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

希望对你有帮助。

【讨论】:

  • 这绝对有帮助,而且我首先弄清楚了为什么会出现错误,这与脚本和 csv 文件所在的目录有关。我将它们放在一个目录中,在该目录中,该代码行没有您的修改就没有错误。现在我得到了这个错误:80072030 服务器上没有这样的对象。 Set oContainer = GetObject("LDAP://ou=Test," & _ 我知道事实上我们的一个 OU 被命名为 Test....
  • 我昨天真的开始工作了。如果有人想要代码,请告诉我。问题与我运行脚本的文件夹有关。代码实际上没问题。我还修改了代码以提示用户输入他们希望在其中创建新帐户的 OU 的名称。完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多