Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Edit Computer Object Description in AD

Keyster86 (IS/IT--Management) (OP)
23 Nov 08 9:03
Whom it may concern: I am working on a script that reads a text file containing computer names on each. And for each computer in the given text file, the script will find the MAC Address, IP address, Serial Number for that computer.

Now my problem is this issue. I want to then take that read computer name, search AD (Active Directory) for the corresponding (matching) computer number and then change that computer description field.

How is the best way to do this task? Is ADO the way to go? Could someone through some code sample together for me?

Here is what I have so far...any thoughts?

CODE

' ------------------------------------------------------'
' Version 1.1 - NOVEMBER 2008
' Compiled by SPC Key, John - United States Army
' Disclaimer: Parts of this script were found via
' internet websites, and modified slightly to assist in requirement.
'
' Any use/editing of this VBScript is authorized solely
' based that the maker/creater of this VBScript is not
' held accountable and/or obligated to any wrong actions.
'
'
' NOTES: '
'
' CHANGE LOG:
'
' ------------------------------------------------------'

' Force declare
OPTION EXPLICIT

' Constants
CONST FOR_READ = 1
CONST FOR_WRITE = 2

' DIM Objects : Be sure to set objects to "NOTHING" at end of script.
DIM objFSO, objFile, objCSV, objWMIService, objItem, objNic, objNicConfig

' DIM strings
DIM strUNIT, strIMO, strFileSpec, strComputer, strMAC, strIP, strSTN, strFinalAD, strOldDescription, strInput, strIPAddress

' DIM Columns
DIM colItems, colNics, colNicConfigs

' Initialize Variables
' ---------------------------------------------
' Use these lines for your info.
' ---------------------------------------------
strUNIT = "#########################"
strIMO = "SPC John Key DSN: ###-####"
strFileSpec = "AD_HOSTS_SPECS.csv"

' Initialize files for reading and writing
SET objFSO = CreateObject("Scripting.fileSystemObject")
SET objFile = objFSO.OpenTextFile("hosts.txt", FOR_READ, FALSE)
SET objCSV = objFSO.createtextfile(strFileSpec, FOR_WRITE)

    ' Write the CSV header line
    objCSV.Writeline ("HOST NAMES:,DETAILS:")

' Loop through computer list
DO UNTIL objFile.AtEndOfStream
    ' Read the file
    strComputer = objFile.ReadLine
    
    ' Store MAC (Media Access Control), IP (Internet Protocol), and STN (Serial Tag Number) values into respective strings.
    strMAC = FindMAC (strComputer)
    strIP = FindIP (strComputer)
    strSTN = FindSTN (strComputer)
    
    ' Write stored string values into CSV file.
    objCSV.Writeline (strComputer & ",MAC: " & strMAC & " ; IP: " & strIP & " ; SN: " & strSTN & " ; " & strUNIT & " ; " & strIMO)
    
    ' This section I want put into AD description field.
    strFinalAD = strOldDescription & " ; " & strMAC & " ; " & strIP & " ; " & strSTN & " ; " & strUNIT & " ; " & strIMO

LOOP

' Close report file
objCSV.close

' Clean memory and quit
ScriptEnd

FUNCTION FindMAC(strComputer)
    ' Continue with script if error occures.
    ON ERROR RESUME NEXT
    
    ' When strComputer does not equal blank (null), then strInput is true.
    ' Personnally, I have not figured this line of code out yet; therefore,
    ' I do not understand its purpose. It causes no harm; therefore, it stays.
    IF strComputer <> "" THEN
        strInput = TRUE
    END IF

    SET objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    SET colItems = objWMIService.ExecQuery _
        ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

    ' Search for MAC address. Once MAC is found, it is stored into the "Function" as a value.
    FOR EACH objItem IN colItems
        FindMAC = objItem.MACAddress
    NEXT
END FUNCTION

FUNCTION FindIP(strComputer)
    SET objWMIService = GetObject( _
        "winmgmts:\\" & strComputer & "\root\cimv2")
    SET colNics = objWMIService.ExecQuery _
        ("Select * From Win32_NetworkAdapter " _
            & "Where NetConnectionID = " & _
            "'Local Area Connection'")
    
    FOR EACH objNic IN colNics
        SET colNicConfigs = objWMIService.ExecQuery _
          ("ASSOCIATORS OF " _
              & "{Win32_NetworkAdapter.DeviceID='" & _
          objNic.DeviceID & "'}" & _
          " WHERE AssocClass=Win32_NetworkAdapterSetting")
        FOR EACH objNicConfig IN colNicConfigs
            FOR EACH strIPAddress IN objNicConfig.IPAddress
                IF LEN(strIPAddress) =< 15 THEN
                FindIP = strIPAddress
                END IF
                
            NEXT
        NEXT
    NEXT
END FUNCTION

FUNCTION FindSTN(strComputer)
    ON ERROR RESUME NEXT
    SET objWMIservice = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    SET colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
    FOR EACH objitem IN colitems
          FindSTN =  objitem.serialnumber
    NEXT
END FUNCTION

FUNCTION ScriptEnd()
    ' Clean memory and quit
    SET objFSO = NOTHING
    SET objFile = NOTHING
    SET objCSV = NOTHING
    SET objWMIService = NOTHING
    SET objItem = NOTHING
    SET objNic = NOTHING
    SET objNicConfig = NOTHING
    wscript.echo "Done!"
    wscript.quit
END FUNCTION  

P.S. Oh by the way, the above script is working currently. The point of this post is to take the above working script one step further and edit the "Description" field in AD (Active Directory). Please help.

V/r,

SPC Key
United States Army

shinedog (MIS)
26 Nov 08 13:12
If you already know the location of the computer object in Active Directory, then you could do something simple like:

CODE

Set objComputer = GetObject("LDAP://CN=" & strComputer & ",CN=Computers,DC=Domain,DC=com")

objComputer.Put "Description" , strMAC & "," & strIP & "," & strSTN
objComputer.SetInfo

If you don't have any idea where the computer object may be in AD, you'd have to search AD for it first.  Something like:

CODE

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='computer' and name = '" & strComputer & "'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
     strDN = objRecordSet.Fields("distinguishedName").Value
     objRecordSet.MoveNext
Loop

Set objComputer = GetObject("LDAP://" & strDN)

objComputer.Put "Description" , strMAC & "," & strIP & "," & strSTN
objComputer.SetInfo


That's how I'd do it anyway.  I'm sure there are other ways.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close