Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net  Enable Active Directory User Account

Posted on 2009-07-16
8
Medium Priority
?
2,168 Views
Last Modified: 2012-05-07
I need to be able to enable an Active Directory user account when I click submit. I have page setup with the following:
1. User select OU member from dropDownList1.
2. ObjUser=dropDownList1.SelectedValue
3. ObjUser is plugged into the Active Direct Search routine as so:
searchMe.Filter = "(&(objectclass=user)(samaccountname=" & objUser & "))"
4. This is used to populate other tables on the page such as the account description etc.

Below is the code for adsearcher. Is there another attribute I need to be pulling during this sub and what would I need to set it to in order to enable the user account?

Thanks in advance!!
Public Sub adSearcher()
        Dim adInfo As New DirectoryServices.DirectoryEntry(strLpath)
        Dim searchMe As New System.DirectoryServices.DirectorySearcher(adInfo)
 
        searchMe.PropertiesToLoad.Add("displayName")
        searchMe.PropertiesToLoad.Add("Description")
        searchMe.Sort.Direction = SortDirection.Ascending
        searchMe.PageSize = 10
        searchMe.Filter = "(&(objectclass=user)(samaccountname=" & objUser & "))"
 
        searchMe.SearchScope = SearchScope.Subtree
        Dim results As SearchResultCollection
        results = searchMe.FindAll()
        Dim result As SearchResult
 
        arrCount = intcount - 1
 
        Dim myDarr1(arrCount, 7) As String
        arrCount = 0
 
        For Each result In results
            intcount = intcount + 1
        Next
        If InStr(result.Properties("DisplayName")(0), "$") Then
 
        Else
 
            Try
                resDescription = result.Properties("description")(0)
            Catch ex As Exception
                resDescription = ""
            End Try
 
            TextBox1.Text = resdescription
 
        End If
       
    End Sub

Open in new window

0
Comment
Question by:JB4375
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24876711
Check the heading number 7 in the following link

http://www.vbdotnetheaven.com/UploadFile/ecabral/ADandNETInVB11112005020216AM/ADandNETInVB.aspx?ArticleID=d4c079fb-0f31-40a4-8917-88e54839c1b5
7. Enable a user account.
 
''' <summary>
''' Method to enable a user account in the AD.
''' </summary>
''' <param name="de"></param>
Private Shared Sub EnableAccount(ByVal de As DirectoryEntry)
'UF_DONT_EXPIRE_PASSWD 0x10000
Dim exp As Integer = CInt(de.Properties("userAccountControl").Value)
de.Properties("userAccountControl").Value = exp Or &H0001
de.CommitChanges()
'UF_ACCOUNTDISABLE 0x0002
Dim val As Integer = CInt(de.Properties("userAccountControl").Value)
de.Properties("userAccountControl").Value = val And Not &H0002
de.CommitChanges()
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:JB4375
ID: 24927423
I get what it's supposed to be doing, however I'm at a loss on how to pass the argument to the Enable_Account Sub. I have a dropdownlist that lists the member of the group, the person selects from that sorted list and then clicks a button to enable the account.
Below is the my code for the drop down, the call, etc.
Thanks
 

Public Sub FillDropdown()
        Dim ldapPath As String = "LDAP://DC=domain,DC=com"
        Dim de As New DirectoryEntry(ldapPath)
        Dim ds As New DirectorySearcher(de)
        ds.PageSize = 1000
        ds.Filter = "(objectCategory=User)"
        Dim colUsers As ArrayList
        colUsers = New ArrayList
        Dim result As SearchResult
 
        For Each result In ds.FindAll()
            colUsers.Add(result.Properties("cn")(0))
        Next result
        colUsers.Sort()
        DropDownList1.DataSource = colUsers
        DropDownList1.DataBind()
        de.Username = "domain\" & DropDownList1.SelectedItem.Text
    End Sub
 
Call Enable_Account(????) No idea....doesn't like de.UserName though
 
Private Shared Sub Enable_Account(ByVal de As DirectoryEntry)
        'UF_DONT_EXPIRE_PASSWD 0x10000
        Dim exp As Integer = CInt(de.Properties("userAccountControl").Value)
        de.Properties("userAccountControl").Value = exp Or &H1
        de.CommitChanges()
        'UF_ACCOUNTDISABLE 0x0002
        Dim val As Integer = CInt(de.Properties("userAccountControl").Value)
        de.Properties("userAccountControl").Value = val And Not &H2
        de.CommitChanges()
    End Sub

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 24932813
You need to pass the de to the sub. When button is clicked, set the de properly. For example
de.Path = "LDAP://192.168.1.1/CN=Users;DC=Yourdomain"

Where you would use the IP address of your AD controller and you domain.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Author Comment

by:JB4375
ID: 24936760
OK.
1. I query AD to fill the drop down with members of an OU.
2. I select and account from the list, and on postback the attribute "account description" of the selected item is displayed in another field.
3. I click submit, and the Enable_Account sub is called.
Since I've pointed to the OU it's located in, and displayed it attributes, I was under the impression that I already had the object itself.  Why am I needing to pass the path again instead of just being able to change one of the object's other attributes?
I feel like I'm missing what seems like a minor detail, but is actually totally necessary for what I'm trying to accomplish.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24956932
Hi,
Sorry for late response. I did not understand your point. You are only passing the DirectoryEntry object to the Enable_Account sub.
0
 
LVL 1

Author Comment

by:JB4375
ID: 24960301
Sorry to be slow on the uptake........
"You are only passing the DirectoryEntry object to the Enable_Account sub."
Is this what I SHOULD be doing, and I'm failing to do so, OR
Is this what I'm currently doing, when I should be doing something else?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24965906
Your code looks absolutly fine. The passing of DirectoryEntry is fine as well.
0
 
LVL 1

Author Closing Comment

by:JB4375
ID: 31604295
Thanks for help and patience. ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question