Link to home
Start Free TrialLog in
Avatar of johnnyjonathan
johnnyjonathan

asked on

Script to change user passwords based on a random password maker

Hi,
i have the attached script provided here.
i am looking for a script that will input a list of users (txt\cvs) and will change their user password according to the attached script (a script made to make random passwords).
' Description: 		Auto Generates a password according to the following rules: 
' Be at least 8 positions in length but up to 10.
' Contain at least one English letter and one digit character
' Should not contain a digit character in the first and last position!(0-9)
' No special characters - lower case letters and digits only
' Must begin and end with a letter
 
Const MinLength = 8
Const MaxLength = 9
 
While ValidatePassword(strPassword) = False
	strPassword = GeneratePassword
Wend
 
CopyToClipboard strPassword
 
MsgBox strPassword 
 
Function GeneratePassword()
 
	strRndPwd = ""
	
	If MinLength = "" Or MaxLength = "" Then
		WScript.Echo "Min and max lengths are not defined. Please set MinLength and MaxLength globally."
		Exit Function
	End If
	
	Randomize
	intLength = Int((MaxLength - MinLength + 1) * Rnd + MinLength)
 
	Const AsciiLower = 32
	Const AsciiUpper = 126
 
	While Len(strRndPwd) <= intLength
		'Randomize
		strChr = Chr(Int((AsciiUpper - AsciiLower + 1) * Rnd + AsciiLower))
		If (Asc(strChr) >= 48 And Asc(strChr) <= 57) Or (Asc(strChr) >= 97 And Asc(strChr) <= 122) Then strRndPwd = strRndPwd & strChr
	Wend
 
	GeneratePassword = strRndPwd
End Function
 
Function ValidatePassword(strValPwd)
	Set objRegEx = CreateObject("VBScript.RegExp")
	boolValid = True
	' Check the length requirement
	If Len(strValPwd) < MinLength Or Len(strValPwd) > MaxLength Then boolValid = False
	' Look for an english character
	If boolValid = True Then
		objRegEx.Pattern = "[a-z]"
		Set colMatches = objRegEx.Execute(strValPwd)  
		If colMatches.Count = 0 Then boolValid = False
	End If
	' Look for a digit
	If boolValid = True Then
		objRegEx.Pattern = "[0-9]"
		Set colMatches = objRegEx.Execute(strValPwd)  
		If colMatches.Count = 0 Then boolValid = False
	End If
	' Look for a digit in the first or last position
	If boolValid = True Then
		strFirstChr = Left(strValPwd, 1)
		strLastChr = Right(strValPwd, 1)
		If IsNumeric(strFirstChr) Or IsNumeric(strLastChr) Then boolValid = False
	End If
	' Prevent specific words from being the password
	arrWords = Array( _
		"help", _
		"desk", _
		"hospital" _
	)
	For Each strWord In arrWords
		If InStr(strValPwd, strWord) > 0 Then boolValid = False
	Next
	' Return the valid code
	ValidatePassword = boolValid
End Function
 
Sub CopyToClipboard(strTextForClipboard)
	Set objIE = CreateObject("InternetExplorer.Application")
	objIE.Navigate("about:blank")
	objIE.document.parentwindow.clipboardData.SetData "text", strTextForClipboard
End Sub

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, to change a user's password, you can use:

Set objUser = GetObject("LDAP://cn=KenMyer,ou=Finance,dc=fabrikam,dc=com")
objUser.SetPassword("i5A2sj*!")

which comes from
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1015.mspx

So, if you read through a CSV, and use an LDAP search to bind to the user, you can change the password.....

I'm short of time right now...if you get stuck, I'll help out later...

Regards,

Rob.
Avatar of johnnyjonathan
johnnyjonathan

ASKER

Hi Rob,
thanks for the direction -  i've tried ading the code below to the script so it will read from each line inside the txt file but i got the error -

Microsoft VBScript runtime error: Input past end of file
anything i'm doing wrong?
the txt file holds all CN names.

Set Userlist = CreateObject("Scripting.FileSystemObject")
Set Disabletemp = Userlist.OpenTextFile("d:\Userlist.txt")
	Do While Not Disabletemp.AtEndOfStream
		Disable =  Disabletemp.readline
		Set objUser = GetObject (Disabletemp.readline)
		objUser.SetPassword(strPassword)
	Loop 
Disabletemp.close
Disable.close
 
MsgBox Done

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
Hi Rob,
thanks for your explanation.
i understand better now what i did wrong.

however, i get the attached error when i try to use the user name or the DN.
something i'm missing?

Untitled.jpg
Sorry, my mistake.
i didn't saw that there was a "DO" missing in line 6.
though now it gives me errors for some users "could not find adspath". the users i'm running the search on are from diffrent OU's
 
Hi Johnny,

Sorry for my delay.  The script is looking through AD by the CN of the user, that you have listed in the text file.  It would probably be best to use the samAccountName (user logon name) of the users, as the content of the text file, then change this line:

            strUserADsPath =  Get_LDAP_User_Properties("user", "cn", strUserCN, "adsPath")


to this

            strUserADsPath =  Get_LDAP_User_Properties("user", "samAccountName", strUserCN, "adsPath")

Regards,

Rob.
Works flawless with the last comment!
Thank you very much!  Deserve every point granted
Hi, that's great. Thanks for the grade.  FYI, using the samAccountName is also best because the AD makes sure that each one is unique, so you will affect only one account at a time.

Regards,

Rob.