Reset passwords of each user in an OU in AD with random passwords that are complex say 8 Apha numerical with a special character and email to users.

Hi,

Reset passwords of each user in an OU in AD with random passwords that are complex say 8 Apha numerical with a special character and email to users.

I need help to reset every users password each to be different and then email the password to user.

A defined data that needs to be in the body with a word attachment. thats going to be common.

regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rlandquistCommented:
If the password is reset, how will the user logon to get their email?
0
bsharathAuthor Commented:
Its not that specific Domain password. its for a different domain but the email will reach him on his current logged in Domain
0
jostranderCommented:
Hello Sharath,

Please try this script and let me know if it works for you.

Thank you,
Joe
Const ADS_SCOPE_SUBTREE = 2

Dim strEmailServer
Dim strEmailFrom
Dim strEmailSubject
Dim strEmailBody
Dim strEmailAttachment
Dim DisplayEcho

Set WshShell=CreateObject("Wscript.Shell")


'---------------------------------------------------------------------------
'	User Variables
'---------------------------------------------------------------------------
strOU="ou=temp,ou=test,dc=somewhere,dc=com"

strEmailServer="myserver1"
strEmailFrom = "someone@somewhere.com"
strEmailSubject = "New Password"
strEmailBody = "Here are your credentials:  "
strEmailAttachment = "c:\myfile.doc"

'True to display echos, false to hide
DisplayEcho = True
'---------------------------------------------------------------------------

If DisplayEcho=True then
	'Force CSCRIPT
	If instr(lcase(wscript.fullname),"wscript") then
		wshshell.run "cmd /k cscript //nologo " & chr(34) & wscript.scriptfullname & Chr(34),1,false
		wscript.quit
	End If
End If


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

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = _
    "SELECT ADsPath,sAMAccountName,CN,mail FROM 'LDAP://" & strOU & "' WHERE objectCategory='User' "

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
	strADsPath=objRecordSet.Fields("ADsPath").Value
	strSAM=objRecordSet.Fields("sAMAccountName").Value
	strCN=objRecordSet.Fields("CN").Value
	strEmail=objRecordSet.Fields("mail").Value
	Output ""
	Output strCN & " (" & strSAM & ")"
	Output strEmail
	
	'Call function to create new password
	strNewPassword=CreatePassword
	
	'Call sub to set password for user
	test = SetPassword(strADsPath,strNewPassword)
	
	If test=False then
		Output "Could not set password for user:  " & strCN
	Else
		Output "Password:  " & strNewPassword
		'Email the user
		SendMail strSAM,strEmail,strNewPassword
	End If
	
	objRecordSet.MoveNext
	
	strADsPath=""
	strSAM=""
	strCN=""
	strEmail=""
	strNewPassword=""
	
Loop

Output ""
Output "Operation complete."


Function CreatePassword
	'Uppercase, lowercase, symbol, number
	'8 chars:  alpha-numeric + number
	
	'3 uppercase...ASCII table 65 to 90
	For i = 1 to 3
		pass=pass & Chr(GetRandom(65,90))
	Next
	
	'3 lowercase...ASCII table 97 to 122
	For j = 1 to 3
		pass=pass & Chr(GetRandom(97,122))
	Next
	
	'1 number
	pass=pass & GetRandom(0,9)

	'1 symbol
	strSymbols="`~!@#$%^&*()_+-={}|[]\:;'<>?,./" & Chr(34)
	pass=pass & mid(strSymbols,GetRandom(1,len(strSymbols)),1)
	
	CreatePassword=pass
	pass=""
End Function

Function GetRandom(intLow,intHigh)	
	Randomize
	GetRandom = Int((intHigh - intLow + 1) * Rnd + intLow)
End Function

Function SetPassword(strADsPath,strNewPassword)
	ON ERROR RESUME NEXT
	
	Set objUser=GetObject(strADsPath)
	objUser.SetPassword(strNewPassword)
	objuser.setinfo
	
	If err.number <> 0 then
		SetPassword=False
	Else
		SetPassword=True
	End If

End Function


Sub SendMail(strSAM,strEmailTo,strNewPassword)
	sep= vbCrLf & "______________________________" & vbCrLf & vbCrLf
	prefix="http://schemas.microsoft.com/cdo/configuration/"
	
	Set objEmail = CreateObject("CDO.Message")
	With objEmail
		.From = strEmailFrom
		.To = strEmailTo
		.Subject = strEmailSubject
		.AddAttachment strEmailAttachment
		.Textbody = strEmailBody & vbCrLf & vbCrLf & sep & _
			"username:  " & strSAM & vbCrLf & vbCrLf & _
			"password:  " & strNewPassword & vbCrLf & vbCrLf
		.Configuration.Fields.Item(prefix & "sendusing") = 2
		.Configuration.Fields.Item(prefix & "smtpserver") = strEmailServer
		.Configuration.Fields.Item(prefix & "smtpserverport") = 25
		.Configuration.Fields.Update
		.Send
	End With

End Sub

Sub Output(txt)
	If DisplayEcho=True then wscript.echo txt
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

bsharathAuthor Commented:
Thank you... WIll this reset the password for each user different passwords?
0
jostranderCommented:
Yes, each password will be unique.
0
bsharathAuthor Commented:
I get this
D:\Reset_Password.vbs(145, 17) CDO.Message.1: The system cannot find the file specified.
0
jostranderCommented:
You mentioned that you wanted to attach a Word document to the email.  If you haven't already, you'll need to specify the path to that file in the "User Variables" area

strEmailAttachment = "c:\myfile.doc"
0
bsharathAuthor Commented:
I have mentioned the file. as
strEmailAttachment = "c:\UserMoveRpt.txt"
0
jostranderCommented:
That should work fine too.  The error you see should only happen if that file does not already exist.  Is it on your c:\ already?  

0
bsharathAuthor Commented:
thanks its perfect i wronly mentione D drive
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.