VBScript上的预期循环错误,用于通过csv将用户添加到AD中

时间:2009-06-28 22:42:54

标签: windows vbscript ldap

我需要将一堆用户添加到AD域,显然不想手动输入。我也不想使用CSVDE,因为我想添加密码。我没有太多的背景写VBScripts所以我在techrepublic网上找到了一个。问题是,当我运行脚本时,我得到一个预期的循环错误,并且csv文件中的用户永远不会被添加。如果我在vbs文件的末尾抛出一个循环线,它会创建用户但是然后给我一个错误,该帐户已经存在,因为它显然是在尝试再次运行该脚本。 那么,是否有人对我需要更改以使该脚本正常运行有任何见解?

' ---------------------------------------------------
' Script: createusersfromcsv.vbs
' Author: Scott Lowe
' Input: CSV file with layout logonname,firstname,lastname,password
' Date: December 21, 2005
' Change log:
' no changes
'----------------------------------------------------

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

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

' Input file location
sCSVFileLocation = "C:\Scripts\" 'KEEP TRAILING SLASH!

' Full path to input file
sCSVFile = sCSVFileLocation&"Book2.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 the Active Directory Users container.
Set oRootLDAP = GetObject("LDAP://rootDSE")
Set oContainer = GetObject("LDAP://cn=Users," & _
oRootLDAP.Get("defaultNamingContext"))

' Allows processing to continue even if an error occurs (i.e. dup user)
' We put this below the CSV and AD information since processing can
' continue with a single bad record, but not if there is a problem with
' the CSV file or AD connection
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 = sLastName&", "&sFirstName
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

' Used only for debugging
'if err.number = -2147019886 then
' msgbox "User logon " & sLogon & "already exists"
'End If

' --------- End of user account creation

1 个答案:

答案 0 :(得分:0)

我从谷歌搜索中找到了答案 最后我需要一个: oRecordSet.MoveNext 环

然后就可以了。