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