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"
'****************************

Friday, January 06, 2006

Add users to a group from a CSV file

Here's another that I'm posting per request. This one comes in handy for me quite often. It can be modified to read in (from the file) samAccountnames, the smtp address, just about anything that can be converted using the name translate method. Here's a link that talks about some of the things you can do with IADsNameTranslate. Read through the script to see what attributes you need to modify to get it to work for you. Enjoy!

'------------------------------------------------------
'Use any and all scripts from this site at your own risk
'Test test test!
'This script will take a csv file of samAccountnames (ntname)
'and add all the users to the specified group.

'By KLW, 08/26/05

On Error Resume Next

Const ADS_PROPERTY_CLEAR = 1
Const ADS_PROPERTY_UPDATE = 2
Const ADS_PROPERTY_APPEND = 3
Const ADS_PROPERTY_DELETE = 4

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

'----------------------------------------------------------
'Replace the below item with the security group
'or dist list you want modified
'Modify the dc=microsoft,dc=com to match your domain
Const strGroup = "LDAP://CN=IT Dept,OU=Distribution " _
& "Lists,OU=Exchange,DC=microsoft,DC=com"
Set objGroup = GetObject(strGroup)

'Replace this item with the list of users
Const strFile = "c:\users.csv"

'---------------------------------------------------------
set objReadFSO = CreateObject ("Scripting.FileSystemObject")
set objUsersFile = objReadFSO.OpenTextFile(strFile)

'---------------------------------------------------------
'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

'------------------------------------------------------
do while objUsersFile.AtEndOfStream = False
strUser = rtrim(Ucase(objUsersFile.readLine))

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

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

objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array(strDNPath)
objGroup.SetInfo
Loop

'----------------------------------------------------
wscript.echo "All Done"

set objUser = Nothing
set objGroup = Nothing
set objUsersFile = Nothing
set objReadFSO = Nothing
set objRootDSE = Nothing
set objTrans = Nothing