Solved

Find employeeID and emploeeNumber from AD using VB.NET

Posted on 2010-09-20
2
1,322 Views
Last Modified: 2012-05-10
Hi,

Is it possible to find out a employeeID and employeeNumber from a specified computer using VB.NET.

I know this is possible using VBS, but I dont know how to convert it. Here is the code for VBS

Thank you
Set objConnection = CreateObject("ADODB.Connection")

Set objCommand =   CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection



objCommand.Properties("Page Size") = 1000

 objCommand.Properties("Searchscope") = strSQL 

    strSearch = LCase(searchbox.value)

    objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://"& FQDN &"' WHERE objectCategory='computer' and employeeNumber='*" & strSearch

    Set objRecordSet = objCommand.Execute

Open in new window

0
Comment
Question by:DanielBlackmore
  • 2
2 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33723085
there's no EmployeeName to computer objects in AD , only employeeID.

to get employeeID of computer object in AD use the following code.
usage:

string id = FindComputerEmployeeID("LDAP://cn=computers,DC=soap,DC=com", "COMP1");

static string FindComputerEmployeeID(string LdapRootPath, String computer)
        {
            try
            {
                DirectorySearcher search = new DirectorySearcher(LdapRootPath);
                search.Filter = "(&(objectCategory=computer)(sAMAccountName=" + computer + "$))";
                search.PropertiesToLoad.Add("employeeID");

                SearchResult result = search.FindOne();

                if (result != null)
                {
                    return result.Properties["employeeID"][0].ToString();
                }
            }
            catch (Exception ex)
            {
                string debug = ex.Message;
            }
            return null;
        }

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33723093
here's in vb.net
Private Shared Function FindComputerEmployeeID(computer As [String]) As String
	Try
		Dim search As New DirectorySearcher("LDAP://cn=computers,DC=soap,DC=com")
		search.Filter = "(&(objectCategory=computer)(sAMAccountName=" & Convert.ToString(computer) & "$))"
		search.PropertiesToLoad.Add("employeeID")

		Dim result As SearchResult = search.FindOne()

		If result IsNot Nothing Then
			Return result.Properties("employeeID")(0).ToString()
		End If
	Catch ex As Exception
		Dim debug As String = ex.Message
	End Try
	Return Nothing
End Function

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now