Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-28-2008, 06:46 AM
budbud208 budbud208 is offline
Registered User
 
Join Date: Aug 2008
Posts: 1
Active Directory

Hi,

I have a problem with the script below I am trying to import users into active directory from an excel spreadsheet it works however what I am trying to achieve is to import them into different ou's reading the ou location from the spreadsheet.

This is the line that is causing me problems the string strOU relates to the specific coloumn in excel which has the ou for users to be imported as they are different for each group of users I am trying to import.

strOUContainer = "ou=strOU,ou=Learners,ou=ACLUsers,ou=ACLBasildon,dc=basildonacc,dc=local"

Full script....

' Create Users.vbs
' VBScript to create users from a spreadsheet
' Author Lee Dudmish
August 20008
-------------------------------------------------------'
' SECTION A - Explicitly declaring 3 types of variables
Option Explicit
' Objects declared :
Dim objRootDSE, objFSO, objShell
Dim objUser, objGroup, objContainer
Dim objExcel, objSheet
' Strings declared :
Dim strCN, strNTSam, strPWD, strOUContainer, strUPN, strEnable
Dim strGroupDN, strLast, strFirst, strDescription, strGroup
Dim strHomeFolder, strHomeDrive, strPathExcel
Dim strNetBIOSDomain, strDomain, strOU
' Integers declared :
Dim intNumusers, intRunError, intRow, intCol

'SECTION B - Where is your spreadsheet. Which OU for users?
' PLEASE ALTER Path to your spreadsheet
strPathExcel = "C:\Scripting\Learners\learners1.xls"
' PLEASE ALTER Active directory OU where users will be created
strOUContainer = "ou=strOU,ou=Learners,ou=ACLUsers,ou=ACLBasildon,dc=basildonacc,dc=local"


'SECTION (Extra) - Preparing the File and Shell objects
' N.B. not needed for the basic script.
'Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objShell = CreateObject("Wscript.Shell")


'SECTION C - Connect to Active Directory
' Get Domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")

' SECTION D - Error Handling (First of several)
' Connect to OU where users to be created.
On Error Resume Next
Err.Clear
Set objContainer = GetObject("LDAP://" & strOUContainer)
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Edit the LDAP path to your OU " & strOUContainer
Wscript.Quit
End If
On Error GoTo 0

'Section E - Connect and Open the Spreadsheet
' Connect to spreadsheet where users are stored
Set objExcel = CreateObject("Excel.Application")
' Open the Speadsheet (Error Handling Section).
On Error Resume Next
Err.Clear
objExcel.Workbooks.Open strPathExcel
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Edit the path to YOUR spreadsheet " & strPathExcel
Wscript.Quit
End If
On Error GoTo 0
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

' SECTION F - DO ... LOOP Until Reading the spreadsheet
' Second row in spreadsheet has attributes so start at row 3
' Check intRow offset numbers
intRow = 3
' Do - Read each row in spreadsheet until reach a blank row
' For each row, create user and set attribute values.
' Loop until Empty cell
Do
' Read values from spreadsheet for this user.
strCN = Trim(objSheet.Cells(intRow, 1).Value)
strPWD = Trim(objSheet.Cells(intRow, 2).Value)
strNTSam = Trim(objSheet.Cells(intRow, 3).Value)
strFirst = Trim(objSheet.Cells(intRow, 4).Value)
strLast = Trim(objSheet.Cells(intRow, 5).Value)
strUPN = Trim(objSheet.Cells(intRow, 6).Value)
strHomeFolder = Trim(objSheet.Cells(intRow, 7).Value)
strHomeDrive = Trim(objSheet.Cells(intRow, 8).Value)
strEnable = Trim(objSheet.Cells(intRow, 9).Value)
strDescription = Trim(objSheet.Cells(intRow, 10).Value)
strGroup = Trim(objSheet.Cells(intRow, 12).Value)
strOU = Trim(objSheet.Cells(intRow, 13).Value)

'SECTION G - Creating the user object
On Error Resume Next
Err.Clear
Set objUser = objContainer.Create("user", "cn=" & strCN)
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Cannot create user with cn: " & strCN
Else
On Error GoTo 0
' Assign mandatory attributes cn and sAMAccountName.
If strNTSam = "" Then
strNTSam = strCN
End If
objUser.sAMAccountName = strNTSam
On Error Resume Next
Err.Clear
objUser.SetInfo
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Unable to create user with NT SAM name: " & strNTSam
Else
' Set password for user.
objUser.SetPassword strPWD
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Unable to set password for user " & strNTSam
End If
On Error GoTo 0
' Enable the user account.
objUser.AccountDisabled = False
If strFirst <> "" Then
objUser.givenName = strFirst
End If

' Assign values to remaining attributes.
If strLast <> "" Then
objUser.sn = strLast
End If
If strUPN <> "" Then
objUser.userPrincipalName = strUPN
End If
If strHomeDrive <> "" Then
objUser.homeDrive = strHomeDrive
End If
If strHomeFolder <> "" Then
objUser.homeDirectory = strHomeFolder
End If
'Guy User attributes
If strDescription <> "" Then
objUser.displayName = strDescription
End If
If strEnable <> "" Then
objUser.userAccountControl = strEnable
End If
' If strGroup <> "" Then
' objUser.memberOf = strGroup
' End If

' Set password expired. Must be changed on next logon.
objUser.pwdLastSet = 0
' Save changes.
On Error Resume Next
Err.Clear
objUser.SetInfo
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Unable to set attributes for user with NT name: " _
& strNTSam
End If
On Error GoTo 0
' Section H - Group Membership

' Groups start in column 12
intCol = 12
' Loop Until empty cell.
Do
strGroupDN = Trim(objSheet.Cells(intRow, intCol).Value)
On Error Resume Next
Err.Clear
Set objGroup = GetObject("LDAP://" & strGroupDN)
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Else
objGroup.Add objUser.AdsPath
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Unable to add user " & strNTSam _
& " to group " & strGroupDN
End If
End If
On Error GoTo 0
' Increment to next group DN.
intCol = intCol + 1
Loop Until objSheet.Cells(intRow, intCol).Value = ""
End If
End If
' Increment to next user.
intRow = intRow + 1
intNumusers = intNumusers + 1
Loop Until objSheet.Cells(intRow, 1).Value = ""
' Section G - Echo confirmation Message
Wscript.Echo intNumusers & " Users created in " & strOUContainer
' Section H - Tidy up Reset objects to nothing
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objUser = Nothing
Set objGroup = Nothing
Set objContainer = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Set objFSO = Nothing
Set objShell = Nothing
Set objRootDSE = Nothing
----------------------------------

I am probably missing something really simple any ideas?

Thanks

Lee
Reply With Quote
  #2  
Old 08-28-2008, 05:05 PM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 585
Hi budbud208,

It is not clear which VanDyke Software product you are using.

Can you let us know which product you are using, and provide us with more details about how the product is involved in your script project?
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:33 AM.