Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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
0
bsharath
Asked:
bsharath
  • 6
  • 3
1 Solution
 
weellioCommented:
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
0
 
Farhan KaziSystems EngineerCommented:
:: ===============
:: 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 ***
0
 
bsharathAuthor Commented:
Farhan
I dont get this.Where in the file should i mention the description?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Farhan KaziSystems EngineerCommented:
:: * 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
0
 
bsharathAuthor Commented:
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..
0
 
bsharathAuthor Commented:
Any help Farhan
0
 
Farhan KaziSystems EngineerCommented:
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 ***
0
 
bsharathAuthor Commented:
farhan

I get this.

Dsquery has reached the default limit.

Will it scan the groups of the root domain ?
0
 
bsharathAuthor Commented:
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 ?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now