?
Solved

Check the name of the local administrator account using vb.net

Posted on 2011-10-10
8
Medium Priority
?
650 Views
Last Modified: 2012-05-12
Hello. I have a tool that does some migration task when a user moves a Windows 7 machine from one location to another. One task is to give the local administrator account a new password based on what is used at the site. This works well except for sites that use the French language pack. Administrator is spelled administrateur.

I want to have the tool check how admin account is spelled. Based on what it finds, it will execute the proper code to set the admin password. I was trying to use environment.username but discovered that is for the logged on user only. Is there a way to check the spelling of the admin account using vb.net?

Thank you for any help.
If Environment.UserName = "administrateur" Then
            Dim user As System.DirectoryServices.DirectoryEntry
            Try
                user = New System.DirectoryServices.DirectoryEntry("WinNT://" + Environment.MachineName + "/administrateur,user")
                user.Invoke("SetPassword", txtLocalAdminPW.Text)
                user.CommitChanges()
            Catch ex As Exception
                MsgBox("Error " + Err.Number.ToString + ".  Failed to set local Administrateur password.", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Windows 7 Migration Tool")
                Me.Cursor = Cursors.Default
                Exit Sub
            End Try
End If

Open in new window

0
Comment
Question by:Lorrec
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36945098
One option is to set the password for both Administrator and administrateur if the languages are limited.
0
 

Author Comment

by:Lorrec
ID: 36945817
Can you explain further? Someone else developed the tool. I have some experience with programming but it is not my main area of expertise.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36947506
You can change the code to


If Environment.UserName = "administrateur" or Environment.UserName = "administrator" Then
            Dim user As System.DirectoryServices.DirectoryEntry
            Try
                user = New System.DirectoryServices.DirectoryEntry("WinNT://" + Environment.MachineName + "/" Environment.UserName & ",user")
                user.Invoke("SetPassword", txtLocalAdminPW.Text)
                user.CommitChanges()
            Catch ex As Exception
                MsgBox("Error " + Err.Number.ToString + ".  Failed to set local Administrateur password.", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Windows 7 Migration Tool")
                Me.Cursor = Cursors.Default
                Exit Sub
            End Try
End If

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lorrec
ID: 36948890
Thank you for the information. I assume this means that the user would have to be logged on as local administrator to run this. Is that correct?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36949589
That is how the current code is working. I just modified it to include both Administrator and administrateur. But yes it would only work if the current logged in user is one of them.

You can use WMI to enumerator users

http://www.codeproject.com/KB/cs/enumerate_system_users.aspx

0
 

Author Comment

by:Lorrec
ID: 36951144
Thank you very much. This was very helpful
0
 

Author Closing Comment

by:Lorrec
ID: 36951146
Thank you
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36951491
Glad to help :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 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