Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit Computer Object Description in AD

Status
Not open for further replies.

Keyster86

IS-IT--Management
Jul 23, 2008
50
US
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)
	
	[COLOR=red]' This section I want put into AD description field.
	strFinalAD = strOldDescription & " ; " & strMAC & " ; " & strIP & " ; " & strSTN & " ; " & strUNIT & " ; " & strIMO[/color]
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top