Solved

Find employeeID and emploeeNumber from AD using VB.NET

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

19 Experts available now in Live!

Get 1:1 Help Now