Tubebube Posted September 20, 2011 Posted September 20, 2011 Sub PreUser checks for existing user name on multiple systems in Array via input box from user. The problem is that it does not change the password. The other part's of this script work. Can anyone Help? ' ------- TEXT USED ---------- Title = "Change User Password" strGroup = "Users" ' ------ END TEXT USED -------- '------ GET INPUTS FROM USER ---------- Dim strUserName, strFullName, strPassword If Msgbox("Would you Like to Add a User?" , vbYesNo, Title) = vbYes then Call GetInputs Sub GetInputs Do strUserName = InputBox("Please Enter User Name.",Title) If strUserName = ""Then If MsgBox("Are you Sure you want to Quit?" , vbYesNo, Title)= VbNo Then End If End if Loop Until strUserName <> "" Do strFullName = InputBox("Please Enter Your Full Name.", Title) If strFullName = "" Then If MsgBox("Are you Sure you want to Quit?" , vbYesNo, Title)= VbNo Then 'Else Call Rename_Computer End If End If Loop Until strFullName <> "" Do strPassword = InputBox("Please Enter Password.",Title) If strPassword="" Then If MsgBox("Are you Sure you want to Quit?" , vbYesNo, Title)= VbNo Then End If End If Loop Until strPassword<>"" Call PreUser() Call AddUser() End Sub ' ------ END INPUTS --------------- ' ----- CODE TO PRECHECK FOR USER ACCT ---------- 'this section just changes the password if the user exists Sub PreUser Dim strComputer, objComputer, objUser 'Dim arrComputers2(1) 'Fixed Size Array 'arrComputers2(0) = "" 'arrComputers2(1) = "" arrComputers = Array("SHI-PC") For Each strComputer In arrComputers Set objComputer = GetObject("WinNT://" & strComputer,"")'GetObject("WinNT://" & strComputer2, "") 'Set objUser = objComputer.create("user", strUserName) objComputer.Filter = Array("user", strUserName) For Each User In objComputer If lcase(User.Name) = lcase(strUserName) Then strComputer = ("SHI-PC") On Error Resume Next Set objUser = GetObject("WinNT://" & strComputer & "/Users") Set objUser = objComputer.create("user", strUserName) If Err = 0 Then objUser.SetPassword "Password1" objUser.Put "PasswordExpired", 1 objUser.SetInfo WScript.Echo User.Name & " already exists and password reset." WScript.Quit End If On Error GoTo 0 End If Next Next End Sub ' ----- END CODEODE TO PRECHECK FOR USER ACCT ---------- Sub AddUser 'Dim strUserName, objNetwork, objComputer arrComputers = Array("SHI-PC") 'Dim arrComputers(1) 'Fixed Size Array 'arrComputers(0) = "" 'arrComputers(1) = "" For Each strComputer In arrComputers Set objComputer = GetObject("WinNT://" & strComputer,"") Set objUser = objComputer.Create("user", strUserName) objUser.SetPassword strPassword objUser.FullName = strFullName objUser.Put "Description", strUserName objUser.Put "PasswordExpired", 1 objUser.SetInfo ' ----- CODE TO ADD USER TO GROUP ------- 'Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup) 'objGroup.Add("WinNT://" & strComputer & "/" & strUserName) ' ----- END CODE TO ADD USER TO GROUP ----- Next Call MsgBox ("User Name: " & strUserName & vbCrLf & "Password: " &strPassword & VbCrLf & VbCrLf & "Please Write This Information Down.", 64, Title) 'Call AddGroup End Sub ' ----- END CODE TO ADD USER ------- '------ EXIT ----- Sub CleanUp Call MsgBox ("Click OK To Exit the Program",0, Title) WScript.quit End Sub End If '----- EXIT ------ ' Needs to be interactive: 'Check to see if user exist, if they exist then reset the password 'If they do not exist then create new user across multiple servers - CHECK 'Set user account to never expire - CHECK 'Set password to change when logon with an option not to change '***Extra- format the account name userid, fullname, description
vikpaw Posted September 20, 2011 Posted September 20, 2011 Do you get any errors at all? What if you set a string variable to store the password, then setpassword that variable..
Tubebube Posted September 20, 2011 Author Posted September 20, 2011 Thank you for responding. I have added a static string variable for the password. Below is my updated code. Also I get no errors. I get the WScript.Echo User.Name & " already exists and password reset." if the user name exists. If the user name does not exist the Sub Adduser routine adds the user name to systems correctly. This is a snapshot of the process. The password does not reset though. Again thank you for responding. ' ------- TEXT USED ---------- Title = "Change User Password" strGroup = "Users" staticPassword = "Tswt5387#0" Dim strComputer, objComputer, objUser ' ------ END TEXT USED -------- '------ GET INPUTS FROM USER ---------- Dim strUserName, strFullName, strPassword If Msgbox("Would you Like to Add a User?" , vbYesNo, Title) = vbYes then Call GetInputs Sub GetInputs Do strUserName = InputBox("Please Enter User Name.",Title) If strUserName = ""Then If MsgBox("Are you Sure you want to Quit?" , vbYesNo, Title)= VbNo Then End If End if Loop Until strUserName <> "" Do strFullName = InputBox("Please Enter Your Full Name.", Title) If strFullName = "" Then If MsgBox("Are you Sure you want to Quit?" , vbYesNo, Title)= VbNo Then 'Else Call Rename_Computer End If End If Loop Until strFullName <> "" Do strPassword = InputBox("Please Enter Password.",Title) If strPassword="" Then If MsgBox("Are you Sure you want to Quit?" , vbYesNo, Title)= VbNo Then End If End If Loop Until strPassword<>"" Call PreUser() Call AddUser() End Sub ' ------ END INPUTS --------------- ' ----- CODE TO PRECHECK FOR USER ACCT ---------- 'this section just changes the password if the user exists Sub PreUser Dim arrComputers(1) 'Fixed Size Array arrComputers(0) = "PC-1" arrComputers(1) = "PC-2" 'arrComputers = Array("SHI-PC") For Each strComputer In arrComputers Set objComputer = GetObject("WinNT://" & strComputer,"") 'Set objUser = objComputer.create("user", strUserName) objComputer.Filter = Array("user", strUserName) For Each User In objComputer If lcase(User.Name) = lcase(strUserName) Then 'strComputer = ("") On Error Resume Next Set objUser = GetObject("WinNT://" & strComputer & "/Users") Set objUser = objComputer.create("user", strUserName) If Err = 0 Then 'objUser.AccountDisabled = True objUser.SetPassword staticPassword objUser.Put "PasswordExpired", 1 objUser.SetInfo WScript.Echo User.Name & " already exists and password reset." WScript.Quit End If On Error GoTo 0 End If Next Next End Sub ' ----- END CODEODE TO PRECHECK FOR USER ACCT ---------- Sub AddUser 'Dim strUserName, objNetwork, objComputer 'arrComputers = Array("SHI-PC") Dim arrComputers(1) 'Fixed Size Array arrComputers(0) = "PC-1" arrComputers(1) = "PC-2" For Each strComputer In arrComputers Set objComputer = GetObject("WinNT://" & strComputer,"") Set objUser = objComputer.Create("user", strUserName) objUser.SetPassword strPassword objUser.FullName = strFullName objUser.Put "Description", strUserName objUser.Put "PasswordExpired", 1 objUser.SetInfo ' ----- CODE TO ADD USER TO GROUP ------- Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup) objGroup.Add("WinNT://" & strComputer & "/" & strUserName) ' ----- END CODE TO ADD USER TO GROUP ----- Next Call MsgBox ("User Name: " & strUserName & vbCrLf & "Password: " &strPassword & VbCrLf & VbCrLf & "Please Write This Information Down.", 64, Title) 'Call AddGroup End Sub ' ----- END CODE TO ADD USER ------- '------ EXIT ----- Sub CleanUp Call MsgBox ("Click OK To Exit the Program",0, Title) WScript.quit End Sub End If '----- EXIT ------ ' Needs to be interactive: 'Check to see if user exist, if they exist then reset the password 'If they do not exist then create new user across multiple servers - CHECK 'Set user account to never expire - CHECK 'Set password to change when logon with an option not to change '***Extra- format the account name userid, fullname, description
vikpaw Posted September 21, 2011 Posted September 21, 2011 I'm no scripting expert, but i have something else for you to try. In my first draft yesterday, my immediate response was going to be to tell you to send the password in as a parameter in brackets, as you would with most functions/methods, but as i've never used SetPassword i thought i'd check. I googled it and saw examples in the way you've done it. I just googled again, and found the following syntax: [font=monospace]objUser.SetPassword("1234xyz")[/font] I think that is the best bet. Maybe you can use either syntax. Or for some reason the way you are doing it is not kicking an error when it should. It looks like you put a text password in quotes and brackets, but if using a variable you don't need to. But if you mess around with it, you should find out what is best. Also, i would cut out all the extra gumpf from the script, until this bit works, so just write a new simple script, that is all hard coded. Best way to troubleshoot. Or put in lots of wscript.echo every few steps, so you can see that it is working along the way. Just doing the bind to an known user object and changing the password as above, should prove it works with the new syntax. Try putting the setInfo directly after changing the password, before doing anything else. Lastly, you do have permission to do this don't you? If not, it will never work. Check these links as well How Can I Change a User Reset local administrator passwords with VBScript | TechRepublic
Tubebube Posted September 21, 2011 Author Posted September 21, 2011 Thanks so much for your help. I have figured out another approach to get this to work. Thanks again.
Tubebube Posted September 21, 2011 Author Posted September 21, 2011 I added this function to make it work. Function CheckIfUserExists(userName) Dim arrComputers(1) 'Fixed Size Array arrComputers(0) = "" arrComputers(1) = "" For Each strComputer In arrComputers Set objComputer = GetObject("WinNT://" & strComputer & "") objComputer.Filter = Array("user") intFound = 0 For Each User In objComputer If lcase(User.Name) = lcase(userName) Then intFound = 1 End If Next Next If intFound = 1 Then CheckIfUserExists = True Else CheckIfUserExists = False End If End Function
vikpaw Posted September 25, 2011 Posted September 25, 2011 Glad it's working, but what about the bit about changing password? That's now a separate thing that operates based on the result of the function? Looking at it that, way, i think maybe the issue was the way you were connecting / creating objects, or more importantly the object type. One is a computer object the other is a user object, and it was getting mixed up. Does that make sense? Using functions will make it easier to work with and troubleshoot at any rate.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now