Solved

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

Posted on 2014-04-15
13
577 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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