Solved

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

Posted on 2014-04-15
13
548 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

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

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

14 Experts available now in Live!

Get 1:1 Help Now