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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble with error trap 1

Status
Not open for further replies.

Jerz

MIS
Sep 10, 2004
102
US
OK,this should be simple, but it seems not to work.

Code:
err.clear
Set objUser = objOU.Create("User", "cn=" & strCommon & "")
If err.number = 0 then

I'd hoped err.number would be 0 if the user created without issue, or some other number if the create failed for whatever reason (duplicate, illegal characters, etc.) What I see is that err.number remains 0 regardless of whether or not the user gets created. Does this somehow make sense that I can't see?
 
Maybe something like this instead?
Code:
Set objUser = objOU.Create("User", "cn=" & strCommon & "")
If objUser Is Nothing Then
  MsgBox "It failed!"
Else
  MsgBox "It worked!"
End If
 
or you can try this:
Code:
app_name = "some app_name"  
Err.Raise 95
err.description = "this is a user-generated error"
err.source = "application: " & app_name 
MsgBox "Error number " & Err.Number & ": " & Err.Description & " - " & _
        err.source

or....
if objuser = nothing then 
   Err.Raise 95
end if 
if err.number > 0 then 
   ........
 
OK, I've tried

Code:
err.clear
Set objUser = objOU.Create("User", "cn=" & Common & "")
If objUser Is Nothing Then
  objFileRpt.writeline "Error creating " &  Common       
Else

Still get no error messages when the create fails, it goes ahead to the 'else' part of the statement.

Also tried:

Code:
err.clear
Set objUser = objOU.Create("User", "cn=" & Common & "")
If Not (objUser Is Nothing) Then

Again, regardless of whether or not the user create succeeds, the 'If Not.....' process along as if the create was successful.

I'm willing to try jfdabi1i's suggestion, but I don't understand what he's recommending.

He does have a 'objUser = nothing' as a trigger. Is that the same as 'objUser is nothing'?

 
OK, I've answered my own question.

Limited testing indicates 'objUser = nothing' is possibly what happens when the user does not get made, as opposed to 'objUser is Nothing', which apparently isn't true in VBSCRIPT even though reality works that way.

So this code now seems to function as designed:

Code:
err.clear
Set objUser = objOU.Create("User", "cn=" & Common & "")
If objUser = Nothing Then
  objFileRpt.writeline "Error creating " &  Common       
Else

BUT, Of course, all I have on my input feed at this point is 'bad' users. It's possible I'm now treating everybody as 'bad'. Tomorrow I should have a legitimate new, good, user to create, and we'll see how it runs.

Thanks for the tips.
 
OK, my posted answer is wrong. 'objUser = nothing' is always true, so this doesn't work either.

Any one have any other ideas?
 
[0] If you do not understand this is utterly non-sense, you have to unlearn to do vbs the wrong way.
>If objUser = Nothing Then
It is universally wrong. And the evaluation of the script block following it is an illusion. That is a consequence of putting on error directive without understanding it for a prolonged time period and keep imagining how the script works.

[1] To create a new user account, you can control the createobject line to start with. If there isn't error, it does not mean the account will be created. It only means the relative distinguished name is acceptable and is unique within the container. To eventually create it, you have to set as well the sAMAccountName which is the mandatory property of user account.

[2] Then if sAMAccountName is character-wise acceptable _and_ that it is unique across the forest, it is finally accepted.

[3] But you still have to commit it, that means you cannot be sure until you issue .setInfo line! If no error until there, you have effectively created a barebone user account.

With what you seem to have, one way to proceed is this.
[tt]
'assumed on error resume next somewhrere above
Set objUser = objOU.Create("User", "cn=" & strCommon & "")
If objUser Is Nothing Then
MsgBox "It failed!"
Else
objUser.put "sAMAccountName","xyz"
objUser.setInfo
if err.number=0 then
MsgBox "It worked!"
else
MsgBox "It failed!"
end if
End If
[/tt]
 
Thanks Tsuji, you've saved me again.

I had no idea, and certainly can't find documentation to support it, but you are correct, no user gets made (or not) until the samaccount is populated and commited. Here's what is working for me now:
Code:
err.clear
Set objUser = objOU.Create("User", "cn=" & strCommon & "")
objUser.Put "sAMAccountName", strUID
objUser.SetInfo
If err.number = 0 then

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top