I have a script to create users.

.......
On Error Resume Next

set objUser = objCIADS.Create("User", "CN=" & strFname & " "
&strLname)
objUser.Put "samAccountName", strAccName
objUser.SetInfo
if (Err.Number<>0) then
Wscript.Echo "User Creation failed for " & strFname & " " &
strLname & vbcrdlf & _
"With Error " & Err.Description & " - continuing"
.......


It works just fine.

If I try and create a user that already exists then
the error event is not triggered. If I omit the On Error Resume Next I
get an error saying the object already exists.

Is this a bug or have I got something wrong?

Re: Error Handling - IADS bug ? by Michael

Michael
Tue Jun 12 18:47:13 CDT 2007

Alan Morris wrote:
> I have a script to create users.
>
> .......
> On Error Resume Next
>
> set objUser = objCIADS.Create("User", "CN=" & strFname & " "
> &strLname)
> objUser.Put "samAccountName", strAccName
> objUser.SetInfo
> if (Err.Number<>0) then
> Wscript.Echo "User Creation failed for " & strFname & " " &
> strLname & vbcrdlf & _

If you also have 'Option Explicit' in effect (a best practice) then the line
above will throw a runtime error if you *really* coded vbcrdlf when you
meant vbCrLf. The Wscript.Echo fails silently because the string
concatenation fails due to an undeclared variable name...

Errors in error checking code in VBScript are problemmatic. If you use 'On
Error GoTo 0' then the Err object is implicitly cleared and tere goes the
evidence.

Here's a suggested errorTrap technique that let's do you 'On Error GoTo 0'
without losing evidence...

Dim e 'as a global scope variable...

On Error Resume Next
x = 1/0 'forced divide by zero error...
errorTrap:On Error goto 0

wscript.echo "Error number:", e.number
wscript.echo "Error number:", e 'same as above via Number default
property...
wscript.echo " description:", e.description
wscript.echo " source:", e.source

'-----------------------------------------------------------

Sub errorTrap()
Set e = new cErrState
End Sub

Class cErrState
Private m_number
Public description, source
Sub Class_initialize
with Err
m_number = .number
description = .description
source = .source
End with
End Sub
Public default Property Get Number()
Number = m_number
End Property
End Class


--
Michael Harris
Microsoft.MVP.Scripting



Re: Error Handling - IADS bug ? by Alan

Alan
Wed Jun 13 04:02:04 CDT 2007

Hey Michael, you are not only sharp eyed but brilliant, I looked at
that statement all day and couldn't see anything wrong.

I did in fact have option explicit at the very top of the script - I
have always assumed that this is a 'public' option and so is respected
by subs/functions - am I correct in this?

I'll look further at your suggestions.

Thanks again.

Alan

On Tue, 12 Jun 2007 16:47:13 -0700, "Michael Harris \(MVP\)"
<mikhar.at.mvps.dot.org> wrote:

>Alan Morris wrote:
>> I have a script to create users.
>>
>> .......
>> On Error Resume Next
>>
>> set objUser = objCIADS.Create("User", "CN=" & strFname & " "
>> &strLname)
>> objUser.Put "samAccountName", strAccName
>> objUser.SetInfo
>> if (Err.Number<>0) then
>> Wscript.Echo "User Creation failed for " & strFname & " " &
>> strLname & vbcrdlf & _
>
>If you also have 'Option Explicit' in effect (a best practice) then the line
>above will throw a runtime error if you *really* coded vbcrdlf when you
>meant vbCrLf. The Wscript.Echo fails silently because the string
>concatenation fails due to an undeclared variable name...
>
>Errors in error checking code in VBScript are problemmatic. If you use 'On
>Error GoTo 0' then the Err object is implicitly cleared and tere goes the
>evidence.
>
>Here's a suggested errorTrap technique that let's do you 'On Error GoTo 0'
>without losing evidence...
>
>Dim e 'as a global scope variable...
>
>On Error Resume Next
> x = 1/0 'forced divide by zero error...
>errorTrap:On Error goto 0
>
>wscript.echo "Error number:", e.number
>wscript.echo "Error number:", e 'same as above via Number default
>property...
>wscript.echo " description:", e.description
>wscript.echo " source:", e.source
>
>'-----------------------------------------------------------
>
>Sub errorTrap()
> Set e = new cErrState
>End Sub
>
>Class cErrState
> Private m_number
> Public description, source
> Sub Class_initialize
> with Err
> m_number = .number
> description = .description
> source = .source
> End with
> End Sub
> Public default Property Get Number()
> Number = m_number
> End Property
>End Class


Re: Error Handling - IADS bug ? by Michael

Michael
Wed Jun 13 14:31:34 CDT 2007

Alan Morris wrote:
> Hey Michael, you are not only sharp eyed but brilliant, I looked at
> that statement all day and couldn't see anything wrong.
>
> I did in fact have option explicit at the very top of the script - I
> have always assumed that this is a 'public' option and so is respected
> by subs/functions - am I correct in this?
>

'Option Explicit' is global and it applies to Sub/Function/Class scope. An
undeclared variable is a *runtime* error in VBScript, unlike it's VB/VBA
cousins where it would be a *compile* time error.

Undeclared variables can sit lurking in a VBScript logic path that is only
conditionally executed and there is no error until that logic path is
actually followed...


--
Michael Harris
Microsoft.MVP.Scripting



Re: Error Handling - IADS bug ? by Alan

Alan
Tue Jun 19 07:32:57 CDT 2007

Michael, Thanks again that explains it, I must admit to having mainly
used VB and am only recently got into VBS.

Regards,




On Wed, 13 Jun 2007 12:31:34 -0700, "Michael Harris \(MVP\)"
<mikhar.at.mvps.dot.org> wrote:

>Alan Morris wrote:
>> Hey Michael, you are not only sharp eyed but brilliant, I looked at
>> that statement all day and couldn't see anything wrong.
>>
>> I did in fact have option explicit at the very top of the script - I
>> have always assumed that this is a 'public' option and so is respected
>> by subs/functions - am I correct in this?
>>
>
>'Option Explicit' is global and it applies to Sub/Function/Class scope. An
>undeclared variable is a *runtime* error in VBScript, unlike it's VB/VBA
>cousins where it would be a *compile* time error.
>
>Undeclared variables can sit lurking in a VBScript logic path that is only
>conditionally executed and there is no error until that logic path is
>actually followed...


Re: Error Handling - IADS bug ? by Alan

Alan
Tue Jun 19 08:40:28 CDT 2007

Michael, if you don't mind one subsidiary questions.

What is the easiest way to manage file security in a script, my script
is to create users and their home share, but I want to set appropriate
security on it. I have been looking at XCACLS that seems to use
WBemScripting. In a VBS program I wrote I used the basic windows API
library e.g. GetFileSecurity. I presume that there is not a way to do
this in a script?

Regards,

On Wed, 13 Jun 2007 12:31:34 -0700, "Michael Harris \(MVP\)"
<mikhar.at.mvps.dot.org> wrote:

>Alan Morris wrote:
>> Hey Michael, you are not only sharp eyed but brilliant, I looked at
>> that statement all day and couldn't see anything wrong.
>>
>> I did in fact have option explicit at the very top of the script - I
>> have always assumed that this is a 'public' option and so is respected
>> by subs/functions - am I correct in this?
>>
>
>'Option Explicit' is global and it applies to Sub/Function/Class scope. An
>undeclared variable is a *runtime* error in VBScript, unlike it's VB/VBA
>cousins where it would be a *compile* time error.
>
>Undeclared variables can sit lurking in a VBScript logic path that is only
>conditionally executed and there is no error until that logic path is
>actually followed...


Re: Error Handling - IADS bug ? by Michael

Michael
Tue Jun 19 18:51:52 CDT 2007

Alan Morris wrote:
> Michael, if you don't mind one subsidiary questions.
>
> What is the easiest way to manage file security in a script, my script
> is to create users and their home share, but I want to set appropriate
> security on it. I have been looking at XCACLS that seems to use
> WBemScripting. In a VBS program I wrote I used the basic windows API
> library e.g. GetFileSecurity. I presume that there is not a way to do
> this in a script?

I'm not necessarily the best source of advice for this one - the limited
security scripting I do is at work where we use a purchased 3rd party
toolset that includes a COM interface.

On occasion I have also used subinacl.exe from the windows res kit.

I've never had the need to use xcacls.vbs...


--
Michael Harris
MVP- Admin Frameworks