Solved

Script to change admin password

Posted on 2013-11-05
10
563 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

18 Experts available now in Live!

Get 1:1 Help Now