I need your help again. :)
I'm trying to create some error handling and logging. I've moved some
things around. At one point it was logging all the success' but not the
failures.
Now it creates the log but does nothing else.
I know I've got something hosed up. I just need you to tell me what it is.
Thanks,
Roger
Option Explicit
Const ADS_PROPERTY_CLEAR = 1
Const ADS_PROPERTY_UPDATE = 2
Const ADS_UF_ACCOUNTDISABLE = 2
Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strUserCN,
strUserOU, strUPN, strSAM, strContainer, strName, strErr, strErrReason,
strErrCode 'strNSN, strFullName, strBoss, strMailCode, strJobTitle,
strAssignedID, strLocation, strState, strMove
Dim objUser, objOU, objContainer, objRootDSE, objOrganizationalunit, objFSO,
objCreateFile, objFile
Dim intUAC
' Check for required arguments.
If Wscript.Arguments.Count < 1 Then
Wscript.Echo "Argument <SpreadsheetName> required. For example:" _
& vbCrLf _
& "cscript UpdateADUsers.vbs D:\ADUsers\Test.xls"
Wscript.Quit(0)
End If
' Spreadsheet file.
strExcelPath = Wscript.Arguments(0)
' Bind to Excel object.
On Error Resume Next
Err.Clear
Set objExcel = CreateObject("Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
On Error GoTo 0
' Open spreadsheet.
On Error Resume Next
Err.Clear
objExcel.Workbooks.Open strExcelPath
If Err.Number <> 0 Then
Err.Clear
Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
Wscript.Quit
End If
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
' The first row of the spreadsheet is skipped (column headings). Each
' row after the first is processed until the first blank entry in the
' first column is encountered. The first column is the Distinguished
' Name of the user, the second column is the new profilePath. The loop
' binds to each user object and assigns the new value for the attribute.
' intRow is the row number of the spreadsheet.
'Create log file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objCreateFile = objFSO.CreateTextFile("C:\temp\createuser.log")
objCreateFile.Close
Set objFile = objFSO.OpenTextFile("C:\temp\createuser.log", 8)
intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
strUserDN = Trim(objSheet.Cells(intRow, 1).Value)
strUserCN = Trim(objSheet.Cells(intRow, 2).Value)
strUserOU = Trim(objSheet.Cells(intRow, 3).Value)
strUPN = Trim(objSheet.Cells(intRow, 4).Value)
strSAM = Trim(objSheet.Cells(intRow, 5).Value)
strContainer = strUserOU
strName = strUserCN
'***********************************************
'* Connect to a container *
'***********************************************
Set objRootDSE = GetObject("LDAP://rootDSE")
If strContainer = "" Then
Set objContainer = GetObject("LDAP://" & _
objRootDSE.Get("defaultNamingContext"))
Else
Set objContainer = GetObject("LDAP://" & strContainer)
End If
'***********************************************
'* End connect to a container *
'***********************************************
Set objUser = objContainer.Create("user", "cn=" & strName)
objUser.Put "sAMAccountName", strName
objUser.Put "userPrincipalName", strUPN
On Error Resume Next
objUser.SetInfo
If Err.Number <> 0 Then
' Error raised, alert and skip this entry.
strErr = "can't be created"
strErrReason = "User may already exist"
strErrCode = Err.Number
On Error GoTo 0
Else
' No error, continue and assign other attributes.
strErr = "User created successfully"
strErrReason = "Done"
strErrCode = Err.Number
On Error GoTo 0
objUser.SetPassword "password"
intUAC = objUser.Get("userAccountControl")
If intUAC And ADS_UF_ACCOUNTDISABLE Then
objUser.Put "userAccountControl", intUAC XOR ADS_UF_ACCOUNTDISABLE
End If
objFile.WriteLine strName & "^" & strErr & "^" & strErrReason & "^" &
strErrCode
Err.Clear
objUser.SetInfo
WScript.Echo strUserOU
WScript.Echo strUserDN
WScript.Echo strUserCN
WScript.Echo strSAM
WScript.Echo strUPN
On Error GoTo 0
End If
intRow = intRow + 1
Loop
' Close the workbook.
objExcel.ActiveWorkbook.Close
' Quit Excel.
objExcel.Application.Quit
'Close log file
objFile.Close
' Clean up.
Set objUser = Nothing
Set objExcel = Nothing
Set objSheet = Nothing
Wscript.Echo "Done"