Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Change the description of users according to the file.

Hi,

I have a file which has Username : Description.I want a script to update the data as there in the file to the ADS.And a results file if there is any user not found or not changed.

Regards
Sharath
Avatar of William Elliott
William Elliott
Flag of United States of America image

ads = active directory?
i can't test this, as i dn't have AD instlaled where i am at, but i think this may work play around with it and let me know if i mistyped something

strreadfile = "c:\users.txt" 'the file with the list of usernames : descriptions



Const ForReading = 1
Const ADS_PROPERTY_UPDATE = 2
strComputer = "."
'connect to ldap
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
'read text file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (strreadfile, ForReading)
x = 1
Do Until objTextFile.AtEndOfStream
    struser_desc = objTextFile.ReadLine
      y = 1
'parse eachline of the textfile into an array
      For Each strItem In txtParse(struser_desc)
            'first item is username
            if y = 1 then
                  def = "User"
                  struser = trim(strItem)
            else
            'second item is description
                  def = "Description"
                  strdesc = trim(strItem)
            end if
'            Wscript.Echo "Line " & x & vbtab & def & vbtab & trim(strItem)
            y = y +1            
            'find the user object in AD
                  Set objUsers = objWMI.ExecQuery("SELECT adspath FROM ds_user where ds_UserName = " & struser)
                  If objUsers.Count = 0 Then
'                     WScript.Echo "No matching objects found"
                  Else
                  'if found, update the description field
                     For Each objUser in objUsers
                     Set objUser = GetObject _
                        ("LDAP://" & objuser.adspath)
                        objUser.PutEx ADS_PROPERTY_UPDATE, _
                        "description", strdesc
                        objUser.SetInfo
                     Next
                  End If
      Next
Set objUsers = nothing
x = x +1  
Loop

objTextFile.Close

Function txtparse(ByVal strLine)
    ' Function to parse delimited line and return array
    ' of field values.

    Dim arrFields
    Dim blnIgnore
    Dim intFieldCount
    Dim intCursor
    Dim intStart
    Dim strChar
    Dim strValue

    Const QUOTE = """"
    Const QUOTE2 = """"""

    ' Check for empty string and return empty array.
    If (Len(Trim(strLine)) = 0) then
        txtparse = Array()
        Exit Function
    End If

    ' Initialize.
    blnIgnore = False
    intFieldCount = 0
    intStart = 1
    arrFields = Array()

    ' Add "," to delimit the last field.
    strLine = strLine & ":"

    ' Walk the string.
    For intCursor = 1 To Len(strLine)
        ' Get a character.
        strChar = Mid(strLine, intCursor, 1)
        Select Case strChar
            Case QUOTE
                ' Toggle the ignore flag.
                blnIgnore = Not blnIgnore
            Case ":"
                If Not blnIgnore Then
                    ' Add element to the array.
                    ReDim Preserve arrFields(intFieldCount)
                    ' Makes sure the "field" has a non-zero length.
                    If (intCursor - intStart > 0) Then
                        ' Extract the field value.
                        strValue = Mid(strLine, intStart, _
                            intCursor - intStart)
                        ' If it's a quoted string, use Mid to
                        ' remove outer quotes and replace inner
                        ' doubled quotes with single.
                        If (Left(strValue, 1) = QUOTE) Then
                            arrFields(intFieldCount) = _
                                Replace(Mid(strValue, 2, _
                                Len(strValue) - 2), QUOTE2, QUOTE)
                        Else
                            arrFields(intFieldCount) = strValue
                        End If
                    Else
                        ' An empty field is an empty array element.
                        arrFields(intFieldCount) = Empty
                    End If
                    ' increment for next field.
                    intFieldCount = intFieldCount + 1
                    intStart = intCursor + 1
                End If
        End Select
    Next
    ' Return the array.
    txtparse = arrFields
End Function
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bsharath

ASKER

Farhan
I dont get this.Where in the file should i mention the description?
:: * This script require "IDsList.txt" file on C: drive root from where it will pick NT Logins or user CN's
:: * IDsList.txt file should be in following format (colon separated)
::   Like:
::         FKazi:Emp# 0849
::         CN=Farhan Kazi,OU=ITS,DC=TRAINING,DC=COM:Emp# 0849
Farhan.

Help on this Q.

When i run this

Say i have

Vijayv
Vijay r
VijayC

All the descriptions get changed with one emp id..
Any help Farhan
Check following..

:: ===============
:: READ THIS FIRST
:: ===============
:: * This script require "IDsList.txt" file on C: drive root from where it will pick NT Logins or user CN's
:: * IDsList.txt file should be in following format
::   Like:
::         FKazi
::         CN=Farhan Kazi,OU=ITS,DC=TRAINING,DC=COM
:: * Successful run will generate two files "DescChangeRpt.txt" file on C: drive root.
:: * Copy and paste following script in notepad and save it with any name having .bat extension.
:: *** SCRIPT START ***
@Echo Off
SETLOCAL EnableDelayedExpansion

IF NOT EXIST C:\IDsList.txt Goto ShowErr
FOR %%R IN (C:\IDsList.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST C:\DescChangeRpt.txt DEL /F /Q C:\DescChangeRpt.txt

FOR /F "delims=: tokens=1-2" %%a IN ('Type C:\IDsList.txt') Do (
    Echo Checking %%a
      Echo %%a | Find /I "CN=" >NUL
      IF Not ErrorLevel 1 (
            DSQuery user "%%a" >C:\CNTmp.txt) ELSE (
            DSQuery user -SAMID "%%a" >C:\CNTmp.txt)
      Type C:\CNTmp.txt |FIND /I "CN=" >NUL
      IF Not ErrorLevel 1 (
            Type C:\CNTmp.txt |DSMod User -desc "%%b" >>C:\DescChangeRpt.txt
      )ELSE (Echo '%%a' User ID Not Found. >>C:\DescChangeRpt.txt)
)
Goto EndScript
:ShowErr
Echo "C:\IDsList.txt" file does not exist or file is empty!
:EndScript
IF EXIST C:\CNTmp.txt DEL /F /Q C:\CNTmp.txt
ENDLOCAL
:: *** SCRIPT END ***
farhan

I get this.

Dsquery has reached the default limit.

Will it scan the groups of the root domain ?
Dsquery has reached the default limit.

Tis is not fo this script....Sorry..

Any way it Will it scan the groups of the root domain ?