Solved

Script to change admin password

Posted on 2013-11-05
10
570 Views
Last Modified: 2013-11-08
I have created a script to change the Administrator's password. The script works fine in Windows 7, but gives me an error in windows XP.

The line:

<code>Set user = GetObject("WinNT://" & strComputer & "/" & strUser & ",user") </code>

Causes Windows XP to error with:

"You were not connected because a duplicate name exists on the network. Go to Control Panel and change the computer name and try again."

The error is false as the computer name is not a duplicate.

Anyone have a clue as to why it think it is a duplicate computer name?

Thanks in advance.
Pphread

Here is the entire script:
Option Explicit 
Dim ObjFSO, user, strComputer, X, Y, Z, strPW, strPWConf, oIE, bPasswordBoxOkay, bPasswordBoxWait, strUser
strComputer = "LocalHost"
strUser = "Administrator"
Set ObjFSO = CreateObject("Scripting.FileSystemObject") 

Set user = GetObject("WinNT://" & strComputer & "/" & strUser & ",user") 
		REM strPW = PasswordBox("Please enter the new password for Pphread.") 
		REM strPWConf = PasswordBox("Please confirm the password for Pphread.") 

Do While X = 0
	Do While Y = 0
		strPW = PasswordBox("Please enter the new password for " & strUser & ".") 
		If strPW = "" Then
			Wscript.Echo "You must enter a password."
		Else
			Exit Do
		End If
	Loop

	Do While Z = 0
		strPWConf = PasswordBox("Please confirm the password for " & strUser & ".") 
		If strPWConf = "" Then
			Wscript.Echo "You must confirm the password."
		Else
			Exit Do
		End If
	Loop

	If strPW = strPWConf Then
		Wscript.Echo "Passwords Match."
		Exit Do
	Else
		Wscript.Echo "The Passwords do not match, Please Re-enter."
	End If
Loop
	REM If strPW = "" Then
		REM Wscript.Echo "Blank Passwords are note allowed."
		REM WScript.Quit 1
	REM End If

	REM If strPW = strPWConf Then
		REM Wscript.Echo "Passwords Match."
	REM Else
		REM Wscript.Echo "The Passwords do not match, Please Re-enter."
		REM WScript.Quit 1
	REM End If
	
user.setpassword strPW
user.setinfo 


Function PasswordBox(sTitle) 
  set oIE = CreateObject("InternetExplorer.Application") 
  With oIE 
    .FullScreen = False 
    .ToolBar   = False : .RegisterAsDropTarget = False 
    .StatusBar = False : .Navigate("about:blank") 
    While .Busy : WScript.Sleep 200 : Wend 
    With .document 
      With .ParentWindow 
        .resizeto 500,110 
        .moveto .screen.width/2-200, .screen.height/2-50 
      End With 
      .Write("<html><body bgColor=Silver><center>") 
      .Write( sTitle ) 
      .Write("<input type=password id=pass>  " & _ 
               "<button id=but0>Submit</button>") 
      .WriteLn("</center></body></html>") 
      With .ParentWindow.document.body 
        .scroll="no" 
        .style.borderStyle = "outset" 
        .style.borderWidth = "3px" 
      End With 
      .all.but0.onclick = getref("PasswordBox_Submit") 
      .all.pass.focus 
      oIE.Visible = True 
      bPasswordBoxOkay = False : bPasswordBoxWait = True 
      On Error Resume Next 
      While bPasswordBoxWait 
        WScript.Sleep 100 
        if oIE.Visible Then bPasswordBoxWait = bPasswordBoxWait 
        if Err Then bPasswordBoxWait = False 
      Wend 
      PasswordBox = .all.pass.value 
	  .Quit
    End With ' document 
    .Visible = False 
	.Quit
  End With   ' IE 
End Function 


Sub PasswordBox_Submit() 
  bPasswordBoxWait = False 
End Sub

Open in new window

0
Comment
Question by:pphreadr
[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
  • 5
  • 5
10 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 39626232
Hi, instead of using strComputer = "LocalHost", try using strComputer = "."

Regards,

Rob.
0
 

Author Comment

by:pphreadr
ID: 39633736
RobSampson changing the variable strComputer to "." has allowed me to get past the 'Set user' line, but now I get the error

the filename, directory name, or volume label syntax is incorrect. Code 8007007B on the line 49:
user.setpassword strPW
0
 

Author Comment

by:pphreadr
ID: 39634207
OK how about this one. this works, but is makes the password of 123456 become "123456". The new password has the literal quotes embedded in it. How can I get user.password to work? I believe the syntax for this is
user.password "strPW" not user.password strPW

How do I put quotes around a variable without have the quotes become part of it as below?

Wscript.Echo """" & strPW & """"
user.setpassword  """" & strPW & """"
user.setinfo

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 65

Expert Comment

by:RobSampson
ID: 39634672
I can't test it right now but it should just be
User.setPassword strPW

To not have quotes.

Are you putting any odd characters in the password?

Rob.
0
 

Author Comment

by:pphreadr
ID: 39634815
Actually for testing I have been using
123456
nothing at all special about that.
0
 

Author Comment

by:pphreadr
ID: 39635031
RobSampson
I have updated the code to clean it up a little, but stilll get the same error
Option Explicit 
Dim ObjFSO, objUser, X, Y, Z, strPW, strPWConf, oIE, bPasswordBoxOkay, bPasswordBoxWait
Set ObjFSO = CreateObject("Scripting.FileSystemObject") 
Set objUser = GetObject("WinNT://./Administrator, user")

getPassword
Wscript.Echo "strPW = " & strPW
objUser.setPassword strPW
objUser.SetInfo 

Function getPassword
	Do While X = 0
		Do While Y = 0
			strPW = PasswordBox("Please enter the new password for Administrator.") 
			If strPW = "" Then
				Wscript.Echo "You must enter a password."
			Else
				Exit Do
			End If
		Loop

		Do While Z = 0
			strPWConf = PasswordBox("Please confirm the password for Administrator.") 
			If strPWConf = "" Then
				Wscript.Echo "You must confirm the password."
			Else
				Exit Do
			End If
		Loop

		If strPW = strPWConf Then
			Wscript.Echo "Passwords Match."
			Exit Do
		Else
			Wscript.Echo "The Passwords do not match, Please Re-enter."
		End If
	Loop
End Function 

Function PasswordBox(sTitle) 
  set oIE = CreateObject("InternetExplorer.Application") 
  With oIE 
    .FullScreen = False 
    .ToolBar   = False : .RegisterAsDropTarget = False 
    .StatusBar = False : .Navigate("about:blank") 
    While .Busy : WScript.Sleep 200 : Wend 
    With .document 
      With .ParentWindow 
        .resizeto 500,110 
        .moveto .screen.width/2-200, .screen.height/2-50 
      End With 
      .Write("<html><body bgColor=Silver><center>") 
      .Write( sTitle ) 
      .Write("<input type=password id=pass>  " & _ 
               "<button id=but0>Submit</button>") 
      .WriteLn("</center></body></html>") 
      With .ParentWindow.document.body 
        .scroll="no" 
        .style.borderStyle = "outset" 
        .style.borderWidth = "3px" 
      End With 
      .all.but0.onclick = getref("PasswordBox_Submit") 
      .all.pass.focus 
      oIE.Visible = True 
      bPasswordBoxOkay = False : bPasswordBoxWait = True 
      On Error Resume Next 
      While bPasswordBoxWait 
        WScript.Sleep 100 
        if oIE.Visible Then bPasswordBoxWait = bPasswordBoxWait 
        if Err Then bPasswordBoxWait = False 
      Wend 
      PasswordBox = .all.pass.value 
	  .Quit
    End With ' document 
    .Visible = False 
	.Quit
  End With   ' IE 
End Function 


Sub PasswordBox_Submit() 
  bPasswordBoxWait = False 
End Sub

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39635036
You shouldn't need to use .SetInfo
Try this
user.setpassword(strPW)

Open in new window


Also, to change the password on the local computer, use
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Open in new window


Also, all of the examples I'm looking at don't use ",user" in the GetObject call, so you can try removing that.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39635050
I just tested your code as is on my Windows XP test machine, and it worked fine.  Do you have Windows Script Host 5.7 installed on that machine?

Download details: Windows Script 5.7 for Windows XP
http://www.microsoft.com/downloads/details.aspx?FamilyID=47809025-D896-482E-A0D6-524E7E844D81

Rob.
0
 

Author Comment

by:pphreadr
ID: 39635089
RobSampson

I have found the issue. I have been testing on my home Windows 7 computer and everything worked fine. In order to test on Windows XP I have had to test on a computer at work. (This whole thing is for work anyway). It seems that on Windows XP you get an error stating that "the filename, directory name, or volume label syntax is incorrect. Code 8007007B" when you use a password that does not conform to the password policy minimum requirements. I found this out by running the script on a Windows 7 PC at work.

When I run the script with a proper password it works like a champ.

Giving you the credit for the fix.

Thank you for sticking with me.

Gotta love MS errors.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39635115
Oh, password complexity!  Didn't even think of that!

Glad you figured that out.

Thanks for the grade.

Rob.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

734 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