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"

Re: Are you out there Richard M.? by Richard

Richard
Wed Jan 21 13:24:50 CST 2004

Hi,

I believe the code you posted only logs success, as you indicate. I believe
you just have to move the "End If" statement to make it log both success and
failure. I indicate inline below.

A minor point, is that you don't need to use both CreateTextFile and
OpenTextFile. What you do works because you close the file after you create
it, then open it again. The OpenTextFile method takes a third parameter,
which if true, means create the file if it does not already exist. This way,
the file is never overwritten, but always appended, and the code works even
if the file does not yet exist. You can use:

'Create log file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\createuser.log", 8, True)

Now, in your code below, I indicate where the "End If" (and "SetInfo")
should be:

"Roger" <hainesr3@nationwide.com> wrote in message
news:ehsj%239C4DHA.2336@TK2MSFTNGP09.phx.gbl...
> 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


' Place the SetInfo here, to save changes to userAccountControl
objUser.SetInfo

' End the "If" that tests Err.Number.
End If


>
> objFile.WriteLine strName & "^" & strErr & "^" & strErrReason & "^" &
strErrCode

' Err.Clear not needed, and SetInfo moved above


>
> WScript.Echo strUserOU
> WScript.Echo strUserDN
> WScript.Echo strUserCN
> WScript.Echo strSAM
> WScript.Echo strUPN
>


' The "End If" that was here has been moved up.
' I also removed "On Error GoTo 0", as it has been done above.


>
> 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"
>
>