Link to home
Create AccountLog in
Avatar of croustimiel
croustimiel

asked on

Fill Active Directory user Properties fiels with vbs

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.
Avatar of rejoinder
rejoinder
Flag of Canada image

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

Just in case... change line 18 to this;
objUser.Put "ipPhone", userphonenumber
Avatar of croustimiel
croustimiel

ASKER

Hello,

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

The script runs against the entire domain not a single OU.  Is running against a single OU a requirement?
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.
ASKER CERTIFIED SOLUTION
Avatar of rejoinder
rejoinder
Flag of Canada image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account