Scripts Thread, Need Help with Changing Password on multiple systems. in Coding and Web Development; Sub PreUser checks for existing user name on multiple systems in Array via input box from user. The problem is ...
-
20th September 2011, 04:16 AM #1
- Rep Power
- 0
Need Help with Changing Password on multiple systems.
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
-
-
IDG Tech News
-
20th September 2011, 06:20 AM #2 Do you get any errors at all?
What if you set a string variable to store the password, then setpassword that variable..
-
-
20th September 2011, 04:54 PM #3
- Rep Power
- 0
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.
VbscriptCapture6.PNG
' ------- 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
-
-
21st September 2011, 06:40 AM #4 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:
Code:
objUser.SetPassword("1234xyz") 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
-
-
21st September 2011, 10:48 PM #5
- Rep Power
- 0
Thanks so much for your help. I have figured out another approach to get this to work. Thanks again.
-
-
21st September 2011, 10:51 PM #6
- Rep Power
- 0
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
-
-
25th September 2011, 12:02 PM #7 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.
-
SHARE:
Similar Threads
-
By tosca925 in forum How do you do....it?
Replies: 5
Last Post: 26th June 2008, 11:41 PM
-
By Oops_my_bad in forum General Chat
Replies: 3
Last Post: 4th May 2008, 07:26 PM
-
By goodhead in forum Windows
Replies: 10
Last Post: 2nd October 2007, 10:42 AM
-
By projector1 in forum General Chat
Replies: 24
Last Post: 26th February 2007, 01:08 PM
-
Replies: 11
Last Post: 13th October 2005, 07:42 AM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules