从VBScript创建AD用户 - >错误代码0

时间:2015-06-05 06:52:12

标签: vbscript active-directory

我在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
[...]

我收到一个消息框: 无法使用NT名称创建用户:testuser - 错误代码:0(sAMAccountName)

怎么会发生这种情况?我究竟做错了什么? 是0&lt;&gt; 0?!? 也尝试了#34; 0&#34;可以肯定......

更新: 现在 - 感谢@JosefZ我整理了错误代码-2147016651 但这并不能帮助我...... 新法典:

[...]
    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是testuser(没有空格 - 检查过)

strCN是“测试用户”&#39; (当然,没有其他字符之前或之后都没有引号)

1 个答案:

答案 0 :(得分:1)

记录不足On Error GoTo 0语句自动调用Clear方法。因此,请使用

'[...]
        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
'[...]