Need to update user properties in AD

SyFy
SyFy used Ask the Experts™
on
Hello,
I am looking to update user properties in AD.  For example,  We need to add  fill in telephone numbers, Job Titel, Department, Company and Manager's name.  We can
put this information in text file and need a way to update the AD user account using
a script.  I am new to Vbs and any help will be appreciated.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
have you thought about multi-selecting all of those instead of making a script?  i'm all for scripting, but if it's not something you're going to have to run on a regular basis, I feel like multi-selecting out of ADUC would be quicker.  (when you select multiple users at the same time and then right-click and go to properties, there is a subset of attributes that you can edit - all of the ones you mentioned would be available within that subset)
Krzysztof PytkoSenior Active Directory Engineer
Top Expert 2012

Commented:
Do you have possibility to download and install free Quest PowerShell module for AD ? If so, you can use that to update these attributes.
http://www.quest.com/powershell/activeroles-server.aspx

First, create CSV file with these headers and data

login;tel;job;department;company;manager

save this file with data as c:\users.csv

and use this syntax in Quest PS

Import-CSV c:\users -Delimiter ";" | %{ Get-QADUser $_."login" | Set-QADUser -Company $_."company" -Department $_."department" -PhoneNumber $_."tel" -Title $_."job" -Manager $_."manager" }

Open in new window


Regards,
Krzysztof

Commented:
Here's a simple example script that will get you started.  It takes two command line options (userID and value) and updates the user's company field with the value.

So, if it were called ADUpdate.vbs, then from a command prompt you would run
cscript ADUpdate.vbs SMITHB "Widgets Inc." and the user SMITHB would have their company field updated with the value Widgets Inc.

wscript.arguments(0) is the first command line option (SMITHB in the example), and wscript.arguments(1) is the second ("Widgets Inc.").  "Widgets Inc." is enclosed in quotes on the command line because it has a space, otherwise only Widgets would be assigned to wscript.arguments(1).

You can use ADSIEDIT to find the names of the other attributes that you want to update and alter the script to update them as well.  You can have vbscript open a text file or create a batch file with FOR and run the script for each user/values.  If you need more help with that let me know.

'On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)
If wscript.Arguments.count < 2 Then wscript.quit
strUser = wscript.Arguments(0)
NewValue = wscript.Arguments(1)


'Convert the UserID to DN in order to find Distinguished Name
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
strNTName = strDomainName & "\" & strUser
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, strNTName
' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
strUserDN = Replace(strUserDN, "/", "\/")

wscript.echo strUserDN

Set objUser = GetObject ("LDAP://" & strUserDN)

'Update company field
objUser.company = NewValue
objUser.SetInfo

wscript.quit

Open in new window

How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Commented:
To further clarify, objuser.title would be the Title, objuser.telephonenumber would be the phone number, objuser.department... and so on.  So you could have up to wscript.arguments(4).

Once you have the basics working and you can update one user at a time from the command line, you can have vbscript read in a whole text file and process all the users in the text file.  

It would look something like this:
Set fMD = objFSO.OpenTextFile(File, 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

For I = 0 to UBound(arrCMDdata)
  arrLineValues = Split(arrCMDdata(I), ",")
  strUser = arrLineValues(0)
  strTitle = arrLineValues(1)
  strCompany = arrLineValues(2)
Next

Open in new window


I just like to take baby steps to get there.

Ask for help as you go along on this and I'll help you.  This is not a tough thing to do.

One challenge will be the manager field... that's a distinguished name so it's looking for the dn of another user object in AD, and unless you have the Manager's UserID (samaccountName) or distinguished name, you'll be putting that data into a different field.

If you do have that data, there's a different approach to updating that field.  You use a objUser.PutEx 1, "manager", manager'sdistinguishedname instead of objUser.manager = X.

If I have time tonight or tomorrow and you have interest, I'll try to write you a more complete script.  Let me know.

Author

Commented:
Thank you for helping me guys.   I will basically get a dump from our HR guys, in csv format which will be similar to what Krzysztof has.  I may not be allowed to  run any programs from quest site.  

Example,

jdoe,712-452-1236,Admin Assistant,HR,CompanyName,Supervisor

Could you modify the script to load this info from csv file and update the user profile.

Thanks

Commented:
Yes.  I'll work on it later and hopefully should be able to provide it tomorrow.  It is a holiday weekend so it might not be available until Tuesday.

Commented:
Save this code in a text file as UserUpdate.vbs and run it like this:
cscript UserUpdate.vbs HRDump.csv

Change HRDump.csv to whatever the actual file name is (so if it's UserData.csv then run cscript UserUpdate.vbs UserData.csv).

I haven't tested this and won't be able to until Tuesday, but you're welcome to test it yourself if you can't wait.  In either case, before you use this in production, test it with a .CSV that only has 1 or 2 or 3 test entries, so you can prove that it works before using it for the full user list.  Also, I can't guarantee that the manager field will update because it's a distinguished name field that's supposed to reference the manager's AD user object.  But we'll cross that bridge when we come to it.  Here's the code:

On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)

If wscript.Arguments.count < 1 Then wscript.quit

Set fMD = objFSO.OpenTextFile(wscript.arguments(0), 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

For I = 0 to UBound(arrCMDdata)
	arrLineValues = Split(arrCMDdata(I), ",")
	strUser = arrLineValues(0)
	wscript.echo "Updating user " & strUser
	strPhone = arrLineValues(1)
	strTitle = arrLineValues(2)
	strDept = arrLineValues(3)
	strCompany = arrLineValues(4)
	strManager = arrLineValues(5)

	'Convert the UserID to DN in order to find Distinguished Name
	Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
	strNTName = strDomainName & "\" & strUser
	Const ADS_NAME_INITTYPE_GC = 3
	Const ADS_NAME_TYPE_NT4 = 3
	Const ADS_NAME_TYPE_1779 = 1
	Set objTrans = CreateObject("NameTranslate")
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	' Use the Get method to retrieve the RPC 1779 Distinguished Name.
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strUserDN = Replace(strUserDN, "/", "\/")

	Set objUser = GetObject ("LDAP://" & strUserDN)

	'Update fields
	objUser.phonenumber = strPhone
	objUser.title = strTitle
	objUser.department = strDept
	objUser.company = strCompany
	objUser.objUser.PutEx 1, "manager", strManager
	objUser.SetInfo

	strPhone = ""
	strTitle = ""
	strDept = ""
	strCompany = ""
	strManager = ""
	arrLineValues = ""
Next

wscript.echo "All done!"
wscript.quit

Open in new window

Krzysztof PytkoSenior Active Directory Engineer
Top Expert 2012

Commented:
OK, so in this case your best friend are Microsoft DSTools (DSQUERY and DSMOD) which are available by default on each Domain Controller or any machine with RSAT/Administrative Tools installed.

But first, remove a header from CSV file with column names, leave only data as you showed us in your example, then save this query as batch file (with bat or cmd extension) and run in command-line

@echo off
for /f "tokens=1-6 delims=," %%i in (c:\users.csv) do dsquery user -samid %%i | dsmod user -tel %%j -desc %%k -dept %%l -company %%m -mgr %%n -c

Open in new window


and that's all :)

Krzysztof

Author

Commented:
Krzysztof,

I get the following error when I run this command.
%%i was unexpected at this time

Thanks

Commented:
OK, I tested this and everything works.  
Two items to note:
1) Don't have a header line your csv, just the data.
2) The manager value must be the UserID (samAccountName) of the manager or the manager field won't get updated.  The script converts the UserID to the manager's distinguished name (DN), which is what the manager field requires.  So, if you can't get that from the HR dump, then the script might need to be augmented to do a lastname, firstname lookup to find the DN.  Let me know and I'll help.

Code below.  

'On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)

If wscript.Arguments.count < 1 Then wscript.quit

Set fMD = objFSO.OpenTextFile(wscript.arguments(0), 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

For I = 0 to UBound(arrCMDdata)
   If Len(Trim(arrCMDdata(I))) > 0 Then
	arrLineValues = Split(arrCMDdata(I), ",")
	strUser = arrLineValues(0)
	wscript.echo "Updating user " & strUser
	strPhone = arrLineValues(1)
	strTitle = arrLineValues(2)
	strDept = arrLineValues(3)
	strCompany = arrLineValues(4)
	strManager = arrLineValues(5)

	'Convert the UserID to DN in order to find Distinguished Name
	Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
	strNTName = strDomainName & "\" & strUser
	Const ADS_NAME_INITTYPE_GC = 3
	Const ADS_NAME_TYPE_NT4 = 3
	Const ADS_NAME_TYPE_1779 = 1
	Set objTrans = CreateObject("NameTranslate")
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	' Use the Get method to retrieve the RPC 1779 Distinguished Name.
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strUserDN = Replace(strUserDN, "/", "\/")

	Set objUser = GetObject ("LDAP://" & strUserDN)

	strNTName = strDomainName & "\" & strManager
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strManagerDN = Replace(strUserDN, "/", "\/")
	Set objManager = GetObject ("LDAP://" & strManagerDN)
	managerDN = objManager.GET("distinguishedName")

	'Update fields
	objUser.telephonenumber = strPhone
	objUser.title = strTitle
	objUser.department = strDept
	objUser.company = strCompany
	objUser.Put "manager", managerDN

	objUser.SetInfo

	strPhone = ""
	strTitle = ""
	strDept = ""
	strCompany = ""
	strManager = ""
	arrLineValues = ""
   End If
Next

wscript.echo "All done!"
wscript.quit

Open in new window

'On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)

If wscript.Arguments.count < 1 Then wscript.quit

Set fMD = objFSO.OpenTextFile(wscript.arguments(0), 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

For I = 0 to UBound(arrCMDdata)
   If Len(Trim(arrCMDdata(I))) > 0 Then
	arrLineValues = Split(arrCMDdata(I), ",")
	strUser = arrLineValues(0)
	wscript.echo "Updating user " & strUser
	strPhone = arrLineValues(1)
	strTitle = arrLineValues(2)
	strDept = arrLineValues(3)
	strCompany = arrLineValues(4)
	strManager = arrLineValues(5)

	'Convert the UserID to DN in order to find Distinguished Name
	Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
	strNTName = strDomainName & "\" & strUser
	Const ADS_NAME_INITTYPE_GC = 3
	Const ADS_NAME_TYPE_NT4 = 3
	Const ADS_NAME_TYPE_1779 = 1
	Set objTrans = CreateObject("NameTranslate")
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	' Use the Get method to retrieve the RPC 1779 Distinguished Name.
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strUserDN = Replace(strUserDN, "/", "\/")

	Set objUser = GetObject ("LDAP://" & strUserDN)

	strNTName = strDomainName & "\" & strManager
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strManagerDN = Replace(strUserDN, "/", "\/")
	Set objManager = GetObject ("LDAP://" & strManagerDN)
	managerDN = objManager.GET("distinguishedName")

	'Update fields
	objUser.telephonenumber = strPhone
	objUser.title = strTitle
	objUser.department = strDept
	objUser.company = strCompany
	objUser.Put "manager", managerDN

	objUser.SetInfo

	strPhone = ""
	strTitle = ""
	strDept = ""
	strCompany = ""
	strManager = ""
	arrLineValues = ""
   End If
Next

wscript.echo "All done!"
wscript.quit

Open in new window

Author

Commented:
Hi,

I ran the script and get the following error.

UpdateUser.vbs(27, 2) Microsoft VBScript runtime error:
Name redefined: 'E_ADS_PROPERTY_NOT_FOUND'

Commented:
Is it possible that you pasted into your script file twice, so that there's actually two copies of what I posted in there?  The error indicates two instances of line 27, but there's only one in what I posted and it tests OK for me.

Author

Commented:
This is the code I copied

'On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)

If wscript.Arguments.count < 1 Then wscript.quit

Set fMD = objFSO.OpenTextFile(wscript.arguments(0), 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

For I = 0 to UBound(arrCMDdata)
   If Len(Trim(arrCMDdata(I))) > 0 Then
      arrLineValues = Split(arrCMDdata(I), ",")
      strUser = arrLineValues(0)
      wscript.echo "Updating user " & strUser
      strPhone = arrLineValues(1)
      strTitle = arrLineValues(2)
      strDept = arrLineValues(3)
      strCompany = arrLineValues(4)
      strManager = arrLineValues(5)

      'Convert the UserID to DN in order to find Distinguished Name
      Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
      strNTName = strDomainName & "\" & strUser
      Const ADS_NAME_INITTYPE_GC = 3
      Const ADS_NAME_TYPE_NT4 = 3
      Const ADS_NAME_TYPE_1779 = 1
      Set objTrans = CreateObject("NameTranslate")
      objTrans.Init ADS_NAME_INITTYPE_GC, ""
      objTrans.Set ADS_NAME_TYPE_NT4, strNTName
      ' Use the Get method to retrieve the RPC 1779 Distinguished Name.
      strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
      strUserDN = Replace(strUserDN, "/", "\/")

      Set objUser = GetObject ("LDAP://" & strUserDN)

      strNTName = strDomainName & "\" & strManager
      objTrans.Init ADS_NAME_INITTYPE_GC, ""
      objTrans.Set ADS_NAME_TYPE_NT4, strNTName
      strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
      strManagerDN = Replace(strUserDN, "/", "\/")
      Set objManager = GetObject ("LDAP://" & strManagerDN)
      managerDN = objManager.GET("distinguishedName")

      'Update fields
      objUser.telephonenumber = strPhone
      objUser.title = strTitle
      objUser.department = strDept
      objUser.company = strCompany
      objUser.Put "manager", managerDN

      objUser.SetInfo

      strPhone = ""
      strTitle = ""
      strDept = ""
      strCompany = ""
      strManager = ""
      arrLineValues = ""
   End If
Next

wscript.echo "All done!"
wscript.quit

Commented:
Remove line 27 - this one:
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D

It's not necessary.  Give it a try again, and if you get another error, remove the ' at the beginning of line 1 so that On Error Resume Next is not commented (an apostrophe or single quote is the "remark" indicator for vbscript - anything that follows it is ignored by the script engine).

Let me know how it goes.

Author

Commented:
Hi,

I tried but still getting errors , Now I get different errors.
UpdateUsers.vbs(33, 2) (null): 0x80005008

Commented:
What OS are you running this script on, and are you logged in with a userID that has permissions to update records in AD?

Commented:
Our AD environments are probably a little different.  Try replacing the line
objTrans.Init ADS_NAME_INITTYPE_GC, ""

with

objTrans.Init 1, "YourDomainName"

and replace "YourDomainName" with the name of your domain.  The short Pre-Windows 2000 domain name.

The ADS_NAME_INITTYPE_GC tells this function to find and use a Global Catalog to handle the translation, and the new line has it using the first domain controller it can find.

Author

Commented:
Windows 7 and yes my account has access to update records in AD.  I used the powershell and was able to update but I can't use powershell in prod so I can't really use it.

Author

Commented:
I tried that but same results

Commented:
Is your Windows 7 machine joined to the domain?

Author

Commented:
Yes it is.

Author

Commented:
I tried the for loop method posted by Krzysztof , but I get an error "%%i was unexpected at this time"

@echo off
for /f "tokens=1-6 delims=," %%i in (c:\users.csv) do dsquery user -samid %%i | dsmod user -tel %%j -desc %%k -dept %%l -company %%m -mgr %%n -c

Commented:
Well you got me scratching my head.  Do you see the "Updating user" message right after you run it (before you get the error)?

Commented:
Doh!! I think I see the problem.  I'm assuming that you're testing with more than one entry in your CSV file.  Let me make a few changes and re-post.

Sorry for the drag... but I'm confident that we'll get this working soon for you.

Author

Commented:
Yes,  I have two entries.. sorry about that : -)

Commented:
OK, one more time.
On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)

If wscript.Arguments.count < 1 Then wscript.quit

Set fMD = objFSO.OpenTextFile(wscript.arguments(0), 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1


For I = 0 to UBound(arrCMDdata)
   If Len(Trim(arrCMDdata(I))) > 0 Then
	arrLineValues = Split(arrCMDdata(I), ",")
	strUser = arrLineValues(0)
	wscript.echo "Updating user " & strUser
	strPhone = arrLineValues(1)
	strTitle = arrLineValues(2)
	strDept = arrLineValues(3)
	strCompany = arrLineValues(4)
	strManager = arrLineValues(5)

	'Convert the UserID to DN in order to find Distinguished Name
	strNTName = strDomainName & "\" & strUser
	Set objTrans = CreateObject("NameTranslate")
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	' Use the Get method to retrieve the RPC 1779 Distinguished Name.
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strUserDN = Replace(strUserDN, "/", "\/")

	Set objUser = GetObject ("LDAP://" & strUserDN)

	strNTName = strDomainName & "\" & strManager
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strManagerDN = Replace(strUserDN, "/", "\/")
	Set objManager = GetObject ("LDAP://" & strManagerDN)
	managerDN = objManager.GET("distinguishedName")

	'Update fields
	objUser.telephonenumber = strPhone
	objUser.title = strTitle
	objUser.department = strDept
	objUser.company = strCompany
	objUser.Put "manager", managerDN

	objUser.SetInfo

	strPhone = ""
	strTitle = ""
	strDept = ""
	strCompany = ""
	strManager = ""
	arrLineValues = ""
   End If
Next

wscript.echo "All done!"
wscript.quit

Open in new window

Commented:
No need to apologize... it showed me my mistake.  I was sloppy with my testing - I should have had more than 1 entry in my test csv.  Now I understand why you were getting the errors.

Let me know how it goes with this latest version of the script.

Also, I want to make certain that you understand the limitations of the manager value.

Author

Commented:
Sweet !  A for Awsome ! : -)   If I need to update the cell number,  can I just add to this list or it's a complete rewrite.

name,phone,cellphone,fax,Title,department,companyname,supervisor.

jdoe,712-452-1236,712-123-5464,712-125-4567,Admin Assistant,HR,CompanyName,Supervisor

Author

Commented:
Could you please update the script to include name,phone,cellphone,fax,Title,department,companyname,supervisor

Thanks,  Really appreciate your help !  and Yes,  I understand the limitation with the Manager value.
Commented:
Well you would have to add to the script.  As written right now, it assumes that it will be dealing with a CSV structured like:
UserID,phone,title,department,company,manager

If you change the CSV, you have to update the script accordingly.  So, for example if you wanted to add the cellphone and it became the third value in the CSV, then you'd need to add the line
   strMobile = arrLineValues(2)

And then adjust the others to show that they're in different locations in the csv:
   strTitle = arrLineValues(3)
   strDept = arrLineValues(4)
   strCompany = arrLineValues(5)
   strManager = arrLineValues(6)

And you would need to add a line where it's updating fields
   objUser.mobile = strMobile


And finally blank out the value of the new variable before the script loops to the next entry
   strMobile = ""

Does that make sense?

Author

Commented:
Yep,  I did this and it worked.  This is Awosome !  Really appreciate your help with this.
Thanks a bunch.

Commented:
Is that csv format solid?

name,phone,cellphone,fax,Title,department,companyname,supervisor

It's important to know how each field will be listed in the CSV in order for the script to work.

Commented:
Per your request:
On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDomainName = uCase(objNetwork.UserDomain)

If wscript.Arguments.count < 1 Then wscript.quit

Set fMD = objFSO.OpenTextFile(wscript.arguments(0), 1)
arrCMDdata = Split(UCase(fMD.Readall),vbcrlf)
fMD.Close

Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1


For I = 0 to UBound(arrCMDdata)
   If Len(Trim(arrCMDdata(I))) > 0 Then
	arrLineValues = Split(arrCMDdata(I), ",")
	strUser = arrLineValues(0)
	wscript.echo "Updating user " & strUser
	strPhone = arrLineValues(1)
	strMobile = arrLineValues(2)
	strFax = arrLineValues(3)
	strTitle = arrLineValues(4)
	strDept = arrLineValues(5)
	strCompany = arrLineValues(6)
	strManager = arrLineValues(7)

	'Convert the UserID to DN in order to find Distinguished Name
	strNTName = strDomainName & "\" & strUser
	Set objTrans = CreateObject("NameTranslate")
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	' Use the Get method to retrieve the RPC 1779 Distinguished Name.
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strUserDN = Replace(strUserDN, "/", "\/")

	Set objUser = GetObject ("LDAP://" & strUserDN)

	strNTName = strDomainName & "\" & strManager
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, strNTName
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
	strManagerDN = Replace(strUserDN, "/", "\/")
	Set objManager = GetObject ("LDAP://" & strManagerDN)
	managerDN = objManager.GET("distinguishedName")

	'Update fields
	objUser.telephonenumber = strPhone
	objUser.title = strTitle
	objUser.department = strDept
	objUser.company = strCompany
	objUser.mobile = strMobile
	objUser.facsimileTelephoneNumber = strFax
	objUser.Put "manager", managerDN

	objUser.SetInfo

	strPhone = ""
	strTitle = ""
	strDept = ""
	strCompany = ""
	strManager = ""
	strMobile = ""
	strFax = ""
	arrLineValues = ""
   End If
Next

wscript.echo "All done!"
wscript.quit

Open in new window

Author

Commented:
Yes,  HR can dump the data in any format we give them.  Thanks again.

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