This blog is by design...

Monday, January 30, 2006

Change AD info for users from File - vbscript, csv

The following script was written for someone who had an interesting situation. He had a list of numerous users within his domain (not contained within a single OU) whose phone numbers and fax numbers needed to be changed. So, what we started with here was a csv file with a samAccountname (NT style login name), the new phone number and the new fax number in a csv file, in the following example format:
ssmith,999-9999,888-8888

The goal was then to have a script read this file and change the phone number (telephoneNumber) and fax number (facsimileTelephoneNumber) accordingly. In case you don't know, having a vbscript read a file isn't a terribly big deal, but it really doesn't do the best job of processing information on a single line. For example, when we have ssmith,999-9999,888-8888 it isn't the easiest thing to say 'hey, there's actually 3 pieces of data on this line'. What we wind up having to do is use the comma (rather painfully) to extrapolate each piece of information out of the single one line string, assign it to a variable and commit the change to AD.

We begin by reading the file into an array, extracting the information we need for each change by manipulating the string, then using NameTranslate to convert the samAccountName (that we got from the file) into a distinguishedName, bind to the object, insert the new information and commit the changes.

Enjoy!


'**************************************************
On error resume next
'written by Kristina L. Waters
'1/24/2006
'**************************************************
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_USER_PRINCIPAL_NAME = 9
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1 'Distinguished Name

strFile = "c:\users.csv"

'**************************************************
'Determine DNS Domain Name
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

'**************************************************
' Use the NameTranslate object to find the NetBIOS
' domain name from the DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, strDNSDomain
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)

'The following removes the \ as in DOMAIN\ '
strNetBIOSDomain=Left(strNetBIOSDomain,Len(strNetBIOSDomain)-1)
'wscript.Echo strNetBiosDomain


Set fso = CreateObject("Scripting.FileSystemObject")
Set usrFile = Fso.OpenTextFile(strFile)

'make File into an Array
aData = Split(usrFile.Readall,vbcrlf)

For X=0 To UBound(aData)
strData=aData(x) 'Read in each line of array
'wscript.Echo x

'**************************************************
strUser = Left(strData,InStr(strData,",")-1)
'wscript.Echo strUser

objTrans.Init ADS_NAME_TYPE_1779, strNetBIOSDomain
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strUser
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

'**************************************************
set objUser = GetObject("LDAP://"& strUserDN)
'wscript.Echo objUser.displayName

If InStr(LCase(strData),LCase(objUser.samAccountname)) Then

'Remove the username from the string
strData=Mid(strData,InStr(strData,",")+1)

'Extract the phone number
strPhone=Left(strData,InStr(strData,",")-1)

'Extract the fax
strFax=Mid(strData,InStr(strData,",")+1)

objUser.telephoneNumber = strPhone
objUser.facsimileTelephoneNumber = strFax

'uncomment the line (remove the tick at beginning) to commit changes!
'objUser.setinfo info!

End If

'****************************
Next

set objRootDSE = nothing
set objTrans = nothing
set objOu = nothing
set fso = nothing
set usrFile = nothing

wscript.Echo "All Done"
'****************************

2 Comments:

  • kris,

    Is not necessary to put domain name in the script as i am not able to append user info using your script and moreover not getting any error

    pls consider this and let me know your suggestion

    Regards
    Murali

    By Anonymous Anonymous, at 9:21 PM  

  • Kris,

    Further to my post,I am getting below Error,when i ran the script.but i have full permission on domain


    C:\users.vbs(45, 1) (null): Name translation: Could not find the name or insuffi
    cient right to see name.

    By Anonymous Anonymous, at 2:59 AM  

Post a Comment

<< Home