2011-05-11 109 views
0

我試圖使用我在互聯網上找到的腳本,允許在Active Directory中使用VBScript和CSV文件批量創建新用戶帳戶。我沒有使用CSVDE b/c這個腳本也會創建密碼。運行代碼時我一直遇到這個錯誤,我找不到它。誰能幫忙?VBScript錯誤80040E14 FROM子句中的語法錯誤

'********************************************************************* 
' 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 
+0

您需要選擇「FROM」表名,而不是文件路徑。 – SLaks 2011-05-11 17:07:52

+0

它從CSV文件中拉出。 @fmunkert想出了一個解決方法,幫助我確定了我正在運行的文件夾正在搞砸。不管怎麼說,還是要謝謝你。 – djl236 2011-05-11 18:09:00

回答

0

也許sCSVFile包含特殊字符,因此必須轉義這樣的:

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

我希望它幫助。

+0

這絕對有幫助,我想出了爲什麼我首先得到了錯誤,它必須處理腳本和csv文件所在的目錄。我把它們放在一個目錄中,在那個代碼行中沒有修改就沒有錯誤。現在我得到這個錯誤: 80072030服務器上沒有這樣的對象。 '設置oContainer = GetObject的( 「LDAP:// OU =測試,」 &_' 我知道一個事實,我們的OU的一個名爲Test .... – djl236 2011-05-11 18:05:25

+0

事實上,我這個工作昨天,如果有人想讓代碼讓我知道,問題與我運行腳本的文件夾有關,代碼實際上是可以的,我還修改了代碼,以提示用戶輸入名稱他們想要創建新帳戶的OU。完美地工作。 – djl236 2011-05-13 12:21:18