Solved

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

Posted on 2014-04-15
13
573 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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