Solved

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

Posted on 2014-04-15
13
595 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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