• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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
0
bsharath
Asked:
bsharath
  • 5
  • 4
1 Solution
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now