Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript command to change attributes for a user

Posted on 2014-02-03
6
Medium Priority
?
170 Views
Last Modified: 2014-11-11
I am trying to write a script that will retrieve a user from AD and then change various attributes.  I am accessing the user by their cn and looking to change sAMAccount, userPrincipalName and scriptPath.

I have a number of users to change so currently am trying to user a variable for the cn in the
command

Set objUser = GetObject(strStringToExecute)

Open in new window


where strStringToExecute is built as follows
strStringToExecute = char(34) & "LDAP://cn=" & strNameToChange & ", " & strOU & Char(34)

Open in new window

When i write the string out, it looks right so the question is can GetObject accept the variable?

The specific block of vb code is listed below
objCommand.CommandText = _
	"SELECT distinguishedName FROM 'LDAP://DC=ad,DC=brooksbrothersgroup,dc=com' WHERE objectCategory='user' " & _
	"AND cn = '" & strNameToChange & "'"

Set objRecordSet = objCommand.Execute

if objRecordSet.RecordCount > 0 then

	strStringToExecute = char(34) & "LDAP://cn=" & strNameToChange & ", " & strOU & Char(34)
	Set objUser = GetObject(strStringToExecute)

	objUser.userPrincipalName = txtnewUPN(UsersToChangeCtr)
	objUser.sAMAccountName = txtnewSMA(UsersToChangeCtr)
	objUser.scriptPath = txtsp(UsersToChangeCtr)

	objUser.SetInfo

Open in new window


If not, I would welcome advice on how to achieve this.
0
Comment
Question by:amiurchaudry
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 39834843
For your GetObject call, you should be able to use
Set objUser = GetObject("LDAP://" & objRecordset("distinguishedName"))

That would bind to the user you just found, and you can continue to change properties from there.

Regards,

Rob.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question