Link to home
Start Free TrialLog in
Avatar of Andrew Davis
Andrew DavisFlag for Australia

asked on

script to Reset user account details

because i am lazy and dont want to google and value everyone's opinions ;)
i have a site that the manager wants to be able to reset users passwords when need be. so i was hoping to just script this for him. it would simply require him to enter the username and the password would allways be the same. it would also need to set the "user must change password at next logon" and turn off any account lockout's

if there was also a way in command line to get a listing of all users for a given OU that would be great.
Avatar of sirbounty
sirbounty
Flag of United States of America image

Something like this?

'ResetPW.vbs
On Error Resume Next

Set objConn = CreateObject("ADODB.Connection")
Set objCmd =   CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objCmd.ActiveConnection = objConn

objCmd.Properties("Page Size") = 1000
strUser=InputBox("Enter the user account you'd like to reset","Reset Password")
if strUser="" Then
  wscript.echo "A user account must be entered."
  wscript.quit
End If
'Change the next line to reflect your domain:
objCmd.CommandText = _
    "SELECT AdsPath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='user'"  

Set objRS = objCmd.Execute

If not objRS.EOF Then
   Set objUser = GetObject(objRS.Fields("AdsPath").Value)
   objUser.SetPassword("password")
   objUser.pwdLastSet = 0
   objUser.SetInfo
   Wscript.Echo strUser & "'s password was reset."
End If

This assumes the manager has permission to reset passwords, of course...
SB you are insanely fast at this scipting game - its insane! you want to script me a coffee and lift home>?
Avatar of Andrew Davis

ASKER

Love your work SB, but it doesnt work ;(

The code i have is posted below with the exception of replacing the mydomain for my actual domain.

i certainly am no vbs guru but it appears that you set a variable struser via an input box and then that variable is never looked at again, so how are we setting the correct account? i would have thought something like "select user where user = struser"  I know that is like a sql query and the vbs would be completley different but i am just thinking out loud.

------------
On Error Resume Next

Set objConn = CreateObject("ADODB.Connection")
Set objCmd =   CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objCmd.ActiveConnection = objConn

objCmd.Properties("Page Size") = 1000
strUser=InputBox("Enter the user account you'd like to reset","Reset Password")
if strUser="" Then
  wscript.echo "A user account must be entered."
  wscript.quit
End If
'Change the next line to reflect your domain:
objCmd.CommandText = _
    "SELECT AdsPath FROM 'LDAP://dc=domain,dc=local' WHERE objectCategory='user'"  

Set objRS = objCmd.Execute

If not objRS.EOF Then
   Set objUser = GetObject(objRS.Fields("AdsPath").Value)
   objUser.SetPassword("test")
   objUser.pwdLastSet = 0
   objUser.SetInfo
   Wscript.Echo strUser & "'s password was reset."
End If
-------------------
oh; oops i posted it without changing out my domain name. no biggy
I've modified the domain name - just in case...(with my ZA cap on)
Looking at this now..
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Happy to help - thanx for the grade! :^)
Thanks SB. that worked a treat.
one little question with th fault in it at the first draft, it still ran through to the end and came up with "password reset successfull" i think this may be because of the on error resume next, but would it have changed the password on an account somewhere? perhaps a system account?
Ew - you know, looking at it again, you may be right...

Use this version to see what the first response is...then double-check that account (and no, it wouldn't be system, it'd be somewhere in AD).


Set objConn = CreateObject("ADODB.Connection")
Set objCmd =   CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objCmd.ActiveConnection = objConn

objCmd.Properties("Page Size") = 1000
'Change the next line to reflect your domain:
objCmd.CommandText = _
    "SELECT AdsPath FROM 'LDAP://dc=domain,dc=local' WHERE objectCategory='user'"  

Set objRS = objCmd.Execute

If not objRS.EOF Then
   Set objUser = GetObject(objRS.Fields("AdsPath").Value)
   wscript.echo objUser.Name  & " may have had their password reset!"
End If
Sorry about that. :|
I feel bad for leaving that in there...
dont feel bad. you told me how to fix it. It was the administrator account.

i made one change to the main script to add the lines at the end to report if the account doesn't exist. Let me know if there is any problem with that. But it seems to work.

If not objRS.EOF Then
   Set objUser = GetObject(objRS.Fields("AdsPath").Value)
   objUser.SetPassword("test")
'Probably need to add this as well, in case there's an error...
   If err.Number <> 0 Then
      wscript.echo err.Description 'error encountered
      wscript.quit 'Abort operation
   End If
   objUser.pwdLastSet = 0
   objUser.SetInfo
   Wscript.Echo strUser & "'s password was reset."
   'wscript.quit 'Abort operation
else      
   Wscript.Echo "Username " & strUser & " was not found. Please rerun the password reset."
end if
Technically not incorrect...but I'm not sure the statement applies.
I would only change the comment to:

 strUser & " was not found. Please retype the account and rerun the password reset utility."


Of course, we can add a restart option, if you'd like...

Drop the entire bit above into

Reset 'at the top of the code, followed by the next line
Sub Reset

End Sub 'at the bottom

Then underneath your 'else' above...
else
 ' (removed)  wscript.echo strUser & " was not found. the password reset utility."
  strResp=Msgbox(strUser & " not found.  Would you like to retry?", vbYesNo, "Problem with account information")
  If strResp=vbYes Then Reset

that is a lot neater. Thanks mate, you are the script guru.
"you are the script guru"

I've got the shirt to prove it! :^ ) haha...