ASP A.D Query

Associate
Joined
13 Jul 2005
Posts
738
Location
Brisbane
Hi Chaps,

bit of a newb-a-tron to ASP and have the below sub to reset a user's A.D password.

It currently works, but i want it to throw up an error box if the username entered is not recognised by the A.D, and then return to the start of the sub.

Code:
'Reset Password
Sub Btn3_OnClick
Dim User
Dim UserName
UserName = inputbox("Enter the Username you want to change the password for :","UserName","")
		if len(trim(UserName)) = 0 then
		MsgBox "Please specify a username.",,"SMS 2003 Remote Control"
		exit sub
		end if
NewPassword = inputbox("Enter the password you want it changed to :","password","")
Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
Call User.SetPassword(NewPassword)
User.SetInfo
End Sub

Thanks in advance,

Tucks
 
you'd probably need some kind of ADODB connection to Active Directory to query the OU's and loop through the user objects, I did something simialr recently using VBScript and an exchange environment, but the basics should be the same.

You can use the FileSystemObjects to output the results to a file, or perhaps response.write if you are coding a website.

Hopefully this will give you some ideas...

This is the code:

Code:
'Specify the DistinguisedName (DN) of the OU where you want to search
ou = "DC=Mafia,DC=local"

'constansts for AD searching. base searches the OU above the one you specify, Onelevel searches the ou you specify only, subtree searches the ou you specify and all sub ou's

Const ADS_SCOPE_BASE = 0
Const ADS_SCOPE_ONELEVEL = 1
Const ADS_SCOPE_SUBTREE = 2

'Standard code to create an ADODB connection and the command properties
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

'Set the connection properties.
'Encrypts user information when logging into AD. Optionally you can enter a username and password to login, default behaviour is to use your logged in credentials. Uncomment these if you want to use them.
objConnection.Properties("Encrypt Password") = TRUE
objConnection.Properties("ADSI Flag") = 1
'objConnection.Properties("User ID") = "Username"
'objConnection.Properties("Password") = "Password"
'Page size means the number of items per page, without setting this it will go through only 1000 items
objCommand.Properties("Page Size") = 1000
'Sets the scope of the search. BASE searches the OU above the one specified only. ONELEVEL searches the OU specified only. SUBTREE searches the OU specified along with all sub ou's.
objCommand.Properties("Searchscope") = Const ADS_SCOPE_SUBTREE
'How you want the data sorted.
objCommandProperties("Sort On") = "Name"

'use a SQL based query to return specific values from the LDAP path of the OU where the object class is the one specified.
objCommand.CommandText = _
    "SELECT Name, distinguishedname, ADsPath FROM 'LDAP://" & ou & "' WHERE objectClass='User'"
'Execute the query above to return records.
Set objRecordSet = objCommand.Execute

'Go to first record returned
objRecordSet.MoveFirst

'Loop through until there are no records left.
Do Until objRecordSet.EOF

currentuser = objRecordSet.Fields("Name").Value
strUserPath = objRecordSet.Fields("ADsPath").Value
Set objUser = GetObject(strUserPath)

Next

'Move to next record.   
objRecordSet.MoveNext

'Loop if there are more records.
Loop
 
I don't know much about AD but can't you just check if User is nothing after trying to set it with the GetObject comand?
Failing that does the User object have any error properties you could check?

Simon
 
Back
Top Bottom