Solved

VB.NET Query WMI or AD for full name of user logged in

Posted on 2014-04-15
13
560 Views
Last Modified: 2014-05-01
I already have some script using Visual Studio 2012 that retrieves system info from a remote PC and puts the results in individual textbox's.

One of these text boxes contains the currently logged on username.

I need to add another button which searches AD or WMI to get the full name from the username.
i.e. SmithG becomes Gary Smith

I used to be able to do this using VB but I'm a bit stumped in VS2012.

Any ideas?
0
Comment
Question by:fruitloopy
  • 6
  • 6
13 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40001411
You will want to use the System.DirectoryServices namespace. Within this namespace are classes for searching active directory (DirectorySearcher), viewing AD objects (DirectoryEntry) and others.
http://msdn.microsoft.com/en-us/library/system.directoryservices(v=vs.110).aspx

For WMI, you would use the System.Management namespace:
http://msdn.microsoft.com/en-us/library/ms257340(v=vs.80).aspx
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40001461
If SmithG is the logon name then you need to search against the sAMAccountName property.
        Dim searcher As New DirectorySearcher("(&(sAMAccountName=SmithG))")
        Dim result As SearchResult = searcher.FindOne()

        If Not IsNothing(result) Then
            SomeTextBox.Text = result.Properties("DisplayName")(0).ToString()
        End If

Open in new window

0
 
LVL 2

Author Comment

by:fruitloopy
ID: 40003413
Carl Tawn - I dont really understand the code and I'm new to VB.NET.
What method is it using to search?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40003417
It's using LDAP under the hood to query AD. The DirectoryServices namespace in the .Net framework just abstracts away the details.
0
 
LVL 2

Author Comment

by:fruitloopy
ID: 40032115
I get the error "'SearchResult' is not accessible in this context because it is 'Friend'"

How can I fix that?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40032170
Do you have a class called SearchResult in your own code? Try changing:
Dim result As SearchResult = searcher.FindOne()

Open in new window

To:
Dim result As System.DirectoryServices.SearchResult = searcher.FindOne()

Open in new window

0
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.

 
LVL 2

Author Comment

by:fruitloopy
ID: 40032199
No class called searchresult exists. I changed it as directed but now get:
Error      4      Type 'System.DirectoryServices.SearchResult' is not defined.

Sorry if this is an easy fix, I'm learning VB.NET as I build this app.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40032207
Have you included a reference to the System.DirectoryServices assembly into your project?
0
 
LVL 2

Author Comment

by:fruitloopy
ID: 40032236
It is now and that error no longer appears. A new one has arrived though!
Error      4      'FindOne' is not a member of 'System.Management.ManagementObjectSearcher'.
System.Management is referenced and imported
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40032282
What version of the framework are you using?
0
 
LVL 2

Author Comment

by:fruitloopy
ID: 40032362
.NET Framework 4.5
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40032392
Where is it getting System.Management.ManagementObjectSearcher from? The code sample I posted is using the DirectorySearcher object from the System.DirectoryServices namespace. It shouldn't be touching the System.Management namespace at all.
0
 
LVL 2

Author Comment

by:fruitloopy
ID: 40034227
My Bad. Some older code was in there with the line:
Dim searcher As New ManagementObjectSearcher
Changed to DirectorySearcher and now working.

For anyone wanting the full code:

Dim query4 As ObjectQuery
                        query4 = New ObjectQuery("SELECT * FROM Win32_ComputerSystem")
                        Dim searcher4 As ManagementObjectSearcher
                        searcher4 = _
                            New ManagementObjectSearcher(scope, query4)

                        Dim queryCollection4 As ManagementObjectCollection
                        queryCollection4 = searcher4.Get()

                        Dim m4 As ManagementObject
                        For Each m4 In queryCollection4
                            ' Display the remote computer System information
                            txtArchitecture.Text = String.Format(m4("SystemType"))
                            txtUsername.Text = String.Format(m4("UserName"))
                            If txtUsername.Text > "" Then
                                Dim ADUsername As String = txtUsername.Text
                                ADUsername = txtUsername.Text.Remove(0, 3)

                                Dim searcherNow As New DirectorySearcher("(&(sAMAccountName=" & ADUsername & "))")
                                Dim ADresult As System.DirectoryServices.SearchResult = searcherNow.FindOne()

                                If Not IsNothing(ADresult) Then
                                    txtFullName.Text = ADresult.Properties("DisplayName")(0).ToString()
                                Else
                                    MsgBox("Nothing found for " & ADUsername)
                                End If
                            End If

Open in new window


query4 first gathers the username of the logged on user and displays it in the textbox txtUsername.
ADUsername then removes the first 3 characters which in my case is the domain name "UK\" then uses the DirectorySearcher to find the ADUsername to display in the txtFullName textbox.

Thanks for your help!
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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

28 Experts available now in Live!

Get 1:1 Help Now