Fill Active Directory user Properties fiels with vbs

croustimiel
croustimiel used Ask the Experts™
on
Hello,

I have a TXT file with 3 column separate by ";".
LASTNAME;FIRSTNAME;IP_PHONE_NUMBER
I want to fill the field TELEPHONES > IP PHONE for each user in the list.

How can I do that in a vbscript.
Check the firstname and name, and fill the IP Phone number field.

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
rejoinderManager, IT
Top Expert 2008

Commented:
This should work for you.  I recommend that you run this against a few test accounts first before going against a larger list.
Edit line 5 to match you file name.
Const ForReading = 1
Const ADS_PROPERTY_UPDATE = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("users-to-change.txt", ForReading)

Do Until objFile.AtEndOfStream
	arrColumns      = split(objFile.Readline,";")
	userlastname    = arrColumns(0)
	userfirstname   = arrColumns(1)
	userphonenumber = arrColumns(2)
	
	set objRoot = getobject("LDAP://RootDSE")
	domainname = objRoot.get("defaultNamingContext")
	strCN = finduser(userlastname,userfirstname,domainname)
	if strCN <> "" then
		Set objUser = GetObject (strCN) 
		objUser.Put "telephoneNumber", userphonenumber
		objUser.SetInfo
	end if
loop
objFile.Close

wscript.echo "Done."

Function FindUser(Byval UserLastName, Byval UserFirstName, Byval Domain) 
	on error resume next

	set cn  = createobject("ADODB.Connection")
	set cmd = createobject("ADODB.Command")
	set rs  = createobject("ADODB.Recordset")

	cn.open "Provider = ADsDSOObject;"
	
	cmd.activeconnection = cn
	cmd.commandtext = "SELECT ADsPath FROM 'LDAP://" & Domain & "' WHERE sn = '" & UserLastName & "' AND givenName = '" & UserFirstName & "'"
	
	set rs = cmd.execute

	if err <> 0 then
		FindUser = ""
	else
		if not rs.BOF and not rs.EOF then
     			rs.MoveFirst
     			FindUser = rs(0)
		else
			FindUser = ""
		end if
	end if
	cn.close
end function

Open in new window

rejoinderManager, IT
Top Expert 2008

Commented:
Just in case... change line 18 to this;
objUser.Put "ipPhone", userphonenumber

Author

Commented:
Hello,

Where in your script, did I specify the location of my "OU" where the script compare the users name.

rejoinderManager, IT
Top Expert 2008

Commented:
The script runs against the entire domain not a single OU.  Is running against a single OU a requirement?

Author

Commented:
I have a domain with more than 10 000 account (world people), I just want to fill the user in my country (400 account), then I prefer specify in which OU the script must fill the field.
Manager, IT
Top Expert 2008
Commented:
OK, can you please edit line 14 from this...

domainname = objRoot.get("defaultNamingContext")

to this...

domainname = "OU=This is the OU closest to your users,OU=That OUs Parent," &  objRoot.get("defaultNamingContext")

If you want to just enter the OU path yourself in full including the DC stuff you can just set line 14 to be something like this...
domainname = "OU=Level 3,OU=Level 2,OU=Level 1,DC=something,DC=oranother,DC=com"

With 10k users you are no stranger to OU paths I'm sure.  Let me know how you make out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial