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.
VB ScriptActive Directory

Avatar of undefined
Last Comment
rejoinder

8/22/2022 - Mon
rejoinder

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

rejoinder

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

ASKER
Hello,

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
rejoinder

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

ASKER
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
rejoinder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question