ceturtdiena, 2012. gada 29. marts

vbs to import users from excel to Active Directory 2003

VBS script which takes usernames from excel. Excel must be installed on computer, so its best to use desktop with excel installed and log in as administrator.

Option Explicit
Dim objRootLDAP, objContainer, objUser, objShell
Dim objExcel, objSpread, intRow
Dim strUser, strOU, strSheet
Dim strCN, strSam, strFirst, strLast, strPWD
' -----------------------------------------------'
' Important change OU= and strSheet to reflect your domain
' -----------------------------------------------'



strOU = "OU=Chageit ," ' Note the comma
strSheet = "full\path\to.xls"

' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU & _
objRootLDAP.Get("defaultNamingContext"))

' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 1 'Row 1 often contains headings

' Here is the 'DO...Loop' that cycles through the cells
' Note intRow, x must correspond to the column in strSheet
Do Until objExcel.Cells(intRow,1).Value = ""
   strSam = Trim(objExcel.Cells(intRow, 2).Value)
   strCN = Trim(objExcel.Cells(intRow, 1).Value)
   strPWD = Trim(objExcel.Cells(intRow, 3).Value)

   ' Build the actual User from data in strSheet.
   Set objUser = objContainer.Create("User", "cn=" & strCN)
   objUser.sAMAccountName = strSam
   objUser.SetInfo

   ' Separate section to enable account with its password
   objUser.userAccountControl = 66048
   objUser.pwdLastSet = 0
   objUser.SetPassword strPWD
   ' objUser.Put ADS_UF_DONT_EXPIRE_PASSWD
   objUser.SetInfo

intRow = intRow + 1
Loop
objExcel.Quit

WScript.Quit
Thats it only 3 rows of excel. In my case cell 1 is user account name, cell 2 is username and cell 3 is password.  And parameter Password never expires enabled. Of course there are plenty of parameters like
objUser.givenName = strFirst
objUser.sn = strLast you can find them 
I dont need them right now.

Nav komentāru:

Ierakstīt komentāru