I am trying to grab some information about a list of servers into excel files, which works fine. What i am unable to do is trap the error and fill the error into the excel cells. If the bind to the remote machine does not work it still gives the headers instead of the bind failure message.
--------------------------------------------------------------------------- ----------------- If (strComputer <> "") Then Set colAccounts = GetObject("WinNT://" & strComputer & "") Set colGroups = GetObject("WinNT://" & strComputer & "") colAccounts.Filter = Array("user") colGroups.Filter = Array("group") On Error Resume Next Err.Clear If Err.Number <> 0 Then objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer Else objExcel.Cells(1, 1).Value = "User Accounts" objExcel.Cells(1, 2).Value = "Full Name" objExcel.Cells(1, 3).Value = "Last Logon TimeStamp" -------------------------------------
Second part of the script where error trapping is not working, it just shows a blank cell wherever the last logon information is not available. As i already said the rest works just fine.
------------------------------------- intCol = 3 On Error Resume Next Err.Clear If Err.Number = 0 Then objExcel.Cells(intRow, intCol).Value = objUser.LastLogin Else objExcel.Cells(intRow, intCol).Value = "Never" End If ---------------------------------
Any help would be highly appreciated. Thank you and Regards
> I am trying to grab some information about a list of servers into > excel files, which works fine. What i am unable to do is trap the > error and fill the error into the excel cells. If the bind to the > remote machine does not work it still gives the headers instead of the > bind failure message.
> --------------------------------------------------------------------------- ----------------- > If (strComputer <> "") Then > Set colAccounts = GetObject("WinNT://" & strComputer & "") > Set colGroups = GetObject("WinNT://" & strComputer & "") > colAccounts.Filter = Array("user") > colGroups.Filter = Array("group") > On Error Resume Next > Err.Clear > If Err.Number <> 0 Then > objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer > Else > objExcel.Cells(1, 1).Value = "User Accounts" > objExcel.Cells(1, 2).Value = "Full Name" > objExcel.Cells(1, 3).Value = "Last Logon TimeStamp" > -------------------------------------
> Second part of the script where error trapping is not working, it just > shows a blank cell wherever the last logon information is not > available. As i already said the rest works just fine.
> ------------------------------------- > intCol = 3 > On Error Resume Next > Err.Clear > If Err.Number = 0 Then > objExcel.Cells(intRow, intCol).Value = objUser.LastLogin > Else > objExcel.Cells(intRow, intCol).Value = "Never" > End If > ---------------------------------
> Any help would be highly appreciated. Thank you and Regards
> Sajit Nair
The "On Error Resume Next" statement should be just before the statement expected to possibly raise an error. For example: ========= If (strComputer <> "") Then On Error Resume Next Set colAccounts = GetObject("WinNT://" & strComputer) Set colGroups = GetObject("WinNT://" & strComputer) If (Err.Number <> 0) Then On Error GoTo 0 objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer Else
On Error GoTo 0 colAccounts.Filter = Array("user") colGroups.Filter = Array("group") objExcel.Cells(1, 1).Value = "User Accounts" objExcel.Cells(1, 2).Value = "Full Name" objExcel.Cells(1, 3).Value = "Last Logon TimeStamp" ======== The statement "On Error GoTo 0" restores normal error handling, so unexpected errors don't get ignored. The statements "On Error Resume Next" and "On Error GoTo 0" both clear any error condition that exists at the point, so there is no need to use "Err.Clear".
I can't tell what you are doing later, where you use objUser.LastLogin. Possibly it should be similar to: ======= For Each objUser In colAcccounts On Error Resume Next objExcel.Cells(intRow, intCol).Value = objUser.LastLogin If (Err.Number <> 0) Then On Error GoTo 0 objExcel.Cells(intRow, intCol).Value = "Never" End If On Error GoTo 0 Next
Thanks for your response. I made the modifications as suggested. The first error trap fails but the second one does succeed. Below are both parts in complete
-----------------------------
If (strComputer <> "") Then On Error Resume Next Set colAccounts = GetObject("WinNT://" & strComputer) Set colGroups = GetObject("WinNT://" & strComputer) If (Err.Number <> 0) Then On Error GoTo 0 objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer Else On Error GoTo 0 colAccounts.Filter = Array("user") colGroups.Filter = Array("group") objExcel.Cells(1, 1).Value = "User Accounts" objExcel.Cells(1, 2).Value = "Full Name" objExcel.Cells(1, 3).Value = "Last Logon TimeStamp" For Each objUser In colAccounts intRow = intRow + 1 intCol = 1 objExcel.Cells(intRow, intCol).Value = objUser.Name intCol = 2 objExcel.Cells(intRow, intCol).Value = objUser.FullName intCol = 3 On Error Resume Next objExcel.Cells(intRow, intCol).Value = objUser.LastLogin If (Err.Number <> 0) Then On Error GoTo 0 objExcel.Cells(intRow, intCol).Value = "Never" End If On Error GoTo 0 Next intRow = intRow + 3 objExcel.Cells(intRow, 1).Value = "Local Security Groups" objExcel.Cells(intRow, 2).Value = "Group Members" For Each objGroup In colGroups intRow = intRow + 1 intCol = 1 objExcel.Cells(intRow, intCol).Value = objGroup.Name For Each objUser in objGroup.Members intCol = intCol + 1 objExcel.Cells(intRow, intCol).Value = objUser.Name Next Next End If End If ----------------------
> Thanks for the response. I made the modifications as suggested by you. > The first trap is still not working but the second is. > Here is the complete script. > ---------------------------------------------------------
> Set objShell = CreateObject("WScript.Shell") > Set objFS = CreateObject("Scripting.FileSystemObject") > Set objExcel = CreateObject("Excel.Application") > Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject") > objExcel.Visible = True > inputFile = "c:\Servers.txt" > set objTS = objFS.OpenTextFile(inputFile,1) > Workbook = 1 > counter = 1 > Set objWorksheet = objExcel.Workbooks.Add > Do until objTS.AtEndOfStream > strComputer = Trim(objTS.readline) > If counter > 3 Then > set objWorksheet = objExcel.Sheets.Add( , objExcel.WorkSheets > (objExcel.WorkSheets.Count)) > End If > objExcel.worksheets(counter).Activate > objExcel.worksheets(counter).Name = strComputer > intRow = 1 > intCol = 1 > If (strComputer <> "") Then > Set colAccounts = GetObject("WinNT://" & strComputer & "") > If Err <> 0 Then > objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer > Else > objExcel.Cells(1, 1).Value = "Users" > objExcel.Cells(1, 2).Value = "Last Logon" > objExcel.Cells(1, 3).Value = "Groups" > objExcel.Cells(1, 4).Value = "Group Members" > colAccounts.Filter = Array("user") > For Each objUser In colAccounts > intRow = intRow + 1 > intCol = 1 > objExcel.Cells(intRow, intCol).Value = objUser.Name > intCol = 2 > On Error Resume Next > If Err = 0 Then > objExcel.Cells(intRow, intCol).Value = objUser.LastLogin > Else > objExcel.Cells(intRow, intCol).Value = "Never" > End If > Next > intRow = 1 > Set colGroups = GetObject("WinNT://" & strComputer & "") > colGroups.Filter = Array("group") > For Each objGroup In colGroups > intRow = intRow + 1 > intCol = 3 > objExcel.Cells(intRow, intCol).Value = objGroup.Name > For Each objUser in objGroup.Members > intCol = intCol + 1 > objExcel.Cells(intRow, intCol).Value = objUser.Name > Next > Next > End If > End If > counter = counter +1 > Loop > msgBox "Finished" > -----------------------------------------------------------------
> Regards
> Sajit
I would replace this snippet of your code: =========== Set colAccounts = GetObject("WinNT://" & strComputer & "") If Err <> 0 Then objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer Else objExcel.Cells(1, 1).Value = "Users" ============= With this: ============ On Error Resume Next Set colAccounts = GetObject("WinNT://" & strComputer) If (Err.Number) <> 0 Then On Error GoTo 0 objExcel.Cells(1, 1).Value = "Failed to bind to " & strComputer Else On Error GoTo 0 objExcel.Cells(1, 1).Value = "Users" ======== Also, I would replace this snippet from later in your code: =========== On Error Resume Next If Err = 0 Then objExcel.Cells(intRow, intCol).Value = objUser.LastLogin Else objExcel.Cells(intRow, intCol).Value = "Never" End If =========== With this: ======== On Error Resume Next If (Err.Number = 0) Then On Error GoTo 0 objExcel.Cells(intRow, intCol).Value = objUser.LastLogin Else On Error GoTo 0 objExcel.Cells(intRow, intCol).Value = "Never" End If =========== If you do not restore normal error handling, subsequent errors will be ignored, which besides making troubleshooting nearly impossible, could also yield incorrect results.