Solved

Find employeeID and emploeeNumber from AD using VB.NET

Posted on 2010-09-20
2
1,343 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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