FSE dark alex
FSE dark alex

Reputation: 69

Creating AD-Users from VBScript -> Error Code 0

I am running into a strange error in VBScript:

[...]
            objUser.sAMAccountName = strNTName
            On Error Resume Next
            objUser.SetInfo
            If (Err.Number <> 0) Then
                On Error GoTo 0
                Wscript.Echo "Unable to create user with NT name: " & strNTName & " - Error-Code: " & Err.Number & " (sAMAccountName)"
            Else
[...]

Well I get a message box: Unable to create user with NT name: testuser - Error-Code: 0 (sAMAccountName)

How can that happen? What am I doing wrong? Is 0 <> 0?!? Also tried "0" to be sure...


Update: Now - thanks to @JosefZ I sorted out the Error code -2147016651 But that does not help me eigther... New Code:

[...]
    Set objUser = objContainer.Create("user", "cn=" & strCN)
    If (Err.Number <> 0) Then
        On Error GoTo 0
        Wscript.Echo "Unable to create user with cn: " & strCN
    Else
        On Error GoTo 0
        ' Assign mandatory attributes and save user object.
        If (strNTName = "") Then
            strNTName = strCN
        End If
            objUser.sAMAccountName = strNTName
            On Error Resume Next
            objUser.SetInfo
            If (Err.Number <> 0) Then
                Wscript.Echo "Unable to create user with NT name: " & strNTName & " - Error-Code: " & Err.Number & " (sAMAccountName)"
                On Error GoTo 0
            Else
[...]

strNTName is testuser (no spaces - checked that)

strCN is 'Test User' (no other chars before or after and without the quotes of course)

Upvotes: 0

Views: 320

Answers (1)

JosefZ
JosefZ

Reputation: 30113

Poorly documented that On Error GoTo 0 statement calls the Clear method automatically. Therefore, use

'[...]
        objUser.sAMAccountName = strNTName
        On Error Resume Next
        objUser.SetInfo
        If (Err.Number <> 0) Then
            Wscript.Echo "Unable to create user with NT name: " & strNTName & " - Error-Code: " & Err.Number & " (sAMAccountName)"
            On Error GoTo 0
        Else
'[...]

Upvotes: 1

Related Questions