Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error trap for object deletion 1

Status
Not open for further replies.

Jerz

MIS
Sep 10, 2004
102
US
I have a script that deletes computers that works most of the time, but not all of the time:
Code:
'***************************************************
'*           Connect to the Domain                 *
'***************************************************
    Set DomainObj = GetObject("WinNT://" & DomainString)

'***************************************************
'*             Write Log File entry                *
'***************************************************
    If CompAge > 360 Then
      objFileRpt.writeline "Deleted Computer " & Computer & " that has not been accessed in " & CompAge & " days<br>"
      count = count + 1

'***************************************************
'*            Delete the Computer                  * 
'***************************************************
      DomainObj.Delete "computer", CompString
    end if

So I'm trying to trap the errors with the following changes:


Code:
'***************************************************
'*           Connect to the Domain                 *
'***************************************************
    Set DomainObj = GetObject("WinNT://" & DomainString)

'***************************************************
'*            Delete the Computer                  * 
'***************************************************
    If CompAge > 360 Then
      DomainObj.Delete "computer", CompString
wscript.echo err.number

'***************************************************
'*             Write Log File entry                *
'***************************************************
      If Err.Number <> 0 Then
        objFileRpt.writeline "Could not Delete Computer " & Computer & " Please delete manually<br>"
      Else
        objFileRpt.writeline "Deleted Computer " & Computer & " that has not been accessed in " & CompAge & " days<br>"
        count = count + 1
      End If
    end if

My trap doesn't work, because err.number always equals -2147016690 regardless of whether the computer object successfully deleted or not. Shouldn't it be 0 if the DomainObj.Delete is successful?
 
i would guess it should be 0 if success.
try
Err.Clear
before you issue the Delete command, you shouldnt need to but i sometimes try it.

if you cant get any sense from the return code then you might have to try and bind to the deleted computer account to see if it is really gone and get the return code from that?. a bit arse about face but might get what you want
 
Thanks, dude, err.clear is working in my limited test. I never considered it, but my 'test' computer list started with the one that was a problem. I guess it set the err.number, and subsequent successes were irrelevant until it is manually reset.
 
hmm strange. i was under the impression that everytime you issued a new Method call the Err number would be reset automatically!
might explain why i get similar behavour to yours in a script ive been scratching my head with :)
 
for clarification

The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement. The Clear method can be used to explicitly reset Err

so looks like you should always use an Err.Clear before a statement that you wish to trap the error for, unless you use On Error Resume Next before and On Error Goto 0 after...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top