将用户添加到AD - 接收错误

时间:2015-03-13 20:43:18

标签: vb.net active-directory

有人可以帮忙吗?我在C#中写了这个并且它有效,然后我将它转换为vb,现在它将无法工作。非常感谢任何帮助。

Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim objADAM As DirectoryEntry     '  // Binding object.
    Dim objUser As DirectoryEntry   '    // User object.
    Dim strDisplayName As String    '    // Display name of user.
    Dim strPath As String      '   // Binding path.
    Dim strUser As String            '   // User to create.
    Dim strUserPrincipalName As String   '// Principal name of user.
    Dim strUserAccount As String       ' // IE: smiller

    '// Construct the binding string.
    '//path is set to FrontEnd > CHR > Users
    strPath = "LDAP://OU=Users,OU=CHR,OU=FrontEnd,DC=arrows,DC=COM"

    Console.WriteLine("Bind to: {0}", strPath)

    '// Get AD LDS object.
    '  Try
    ' {
    objADAM = New DirectoryEntry(strPath)
    objADAM.RefreshCache()
    '}
    'catch (Exception v)
    ' {
    '    Console.WriteLine("Error:   Bind failed.");
    '    Console.WriteLine("         {0}", v.Message);
    '     return;
    ' }

    '// Specify User.
    strUser = "CN=Miller\\, Shane"

    '//email display name
    strDisplayName = "lastname, firstname"

    '//user id
    strUserAccount = "smiller_"

    '//user login
    strUserPrincipalName = "smiller_"

    'Console.WriteLine("Create:  {0}", strUser)

    '// Create User.
    'Try

    objUser = objADAM.Children.Add(strUser, "user")
    objUser.Properties("displayName").Add(strDisplayName)
    objUser.Properties("userPrincipalName").Add(strUserPrincipalName)
    objUser.Properties("distinguishedName").Add("smiller_") ' // User name

    '//sets first name
    objUser.Properties("givenName").Add("Shane")

    '//sets last name
    objUser.Properties("sn").Add("Miller")

    '//sets user initials
    objUser.Properties("initials").Add("S")

    '//sets users description(office) 
    objUser.Properties("description").Add("Charlotte")

    '//sets users job title
    objUser.Properties("title").Add("Developer I")

    '//sets domain name
    objUser.Properties("sAMAccountName").Add(strUserAccount)

    '//sets IP Phone ( IE: 26024 )
    objUser.Properties("ipPhone").Add("ipPhone")

    ' //sets fax # ( Currently not in use )
    ' //objUser.Properties["facsimileTelephoneNumber"].Add("facsimileTelephoneNumber");

    ' //sets mobile # 
    objUser.Properties("mobile").Add("mobile")

    '//sets home # 
    objUser.Properties("homephone").Add("homephone")

    '//sets company name
    objUser.Properties("company").Add("Arrows")

    '//sets email
    objUser.Properties("mail").Add("email here")

    '//sets work phone #
    objUser.Properties("telephoneNumber").Add("work telephone here")

    '//sets office
    objUser.Properties("physicalDeliveryOfficeName").Add("Set office here - like Charlotte")

    '//set street address
    objUser.Properties("streetAddress").Add("set office address here")

    '//set city
    objUser.Properties("l").Add("set office city here")

    '//set state/province
    objUser.Properties("st").Add("set office state here")

    '//set zip/postal code
    objUser.Properties("postalCode").Add("set office postal code here")

    '//set home folder - H Drive location
    objUser.InvokeSet("homeDrive", "H:")
    objUser.InvokeSet("homeDirectory", "\\\test")

            '//set department
    objUser.Properties("department").Add("department - Information Technology")
    objUser.CommitChanges()



    '//set up domain context

    '//set default password
    Dim pc = New PrincipalContext(ContextType.Domain)
    Dim userPrin = New UserPrincipal(pc)
    userPrin.SetPassword("defaultpassword")

    objUser.CommitChanges()

    '//Enables Account
    Dim val As Integer = CInt(objUser.Properties("userAccountControl").Value)
    objUser.Properties("userAccountControl").Value = val And Not &H2
    objUser.CommitChanges()

    '//----------------------- Moves To AD Grp ( in this case the "CHR Users" Group )
    Dim bindString As String = "LDAP://arrows.com/CN=CHR Users,OU=SecurityGroups,OU=Groups,OU=FrontEnd,DC=arrows, DC=COM"
    Dim newMember As String = strUser + ", OU=Users, OU=CHR, OU=FrontEnd,DC=Arrows, DC=COM"
    Dim ent As DirectoryEntry = New DirectoryEntry(bindString)

    ent.Properties("member").Add(newMember)

    ent.CommitChanges()

    '//----------------------- Moves To AD Grp ( in this case the "CHR Users" Group )
    ' }
    'catch (Exception v)
    '{
    ' Console.WriteLine("Error:   Create failed.")
    'Console.WriteLine("         {0}", v.Message)
    '    return;
    '   }

    '   // Output User attributes.
    'Console.WriteLine("Success: Create succeeded.")
    'Console.WriteLine("Name:    {0}", objUser.Name)
    '    Console.WriteLine("         {0}",
    '      objUser.Properties["displayName"].Value)
    '  Console.WriteLine("         {0}",
    '       objUser.Properties["userPrincipalName"].Value)
    ' Return
    ' }

End Sub

0 个答案:

没有答案
相关问题