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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AD Computer account updates (newbie)

Status
Not open for further replies.

rpyoung

Technical User
Jan 24, 2003
5
GB
Hello,

I would like to run a script eithe at startup or login that pulls the username and model name of a desktop into the descriptio field of the computer object of Active directory...

I have the following code to extract the info:


On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "Model: " & objItem.Model
Wscript.Echo "UserName: " & objItem.UserName
Next

But I'm having real troube Writing to AD

if anyone can help i would be most greatfull

Regards

Rob
 
hmm this might work

Set computer = GetObject("WinNT://" & domain & "/" & machinename & ",computer")
msgbox computer.some_property
computer.some_property = "somevalue"
computer.setinfo




 
' Determine configuration context from RootDSE object.
Set oRoot = GetObject("LDAP://RootDSE")
sConfig = oRoot.Get("ConfigurationNamingContext")
sDNSDomain = oRoot.Get("DefaultNamingContext")

' Use ADO to search AD for ObjectClass nTDSDSA.
Set oCommand = CreateObject("ADODB.Command")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open = "Active Directory Provider"
oCommand.ActiveConnection = oConnection



oCommand.Properties("Page Size") = 100
oCommand.Properties("Timeout") = 30
oCommand.Properties("Searchscope") = 2
oCommand.Properties("Cache Results") = False
strQ = "SELECT * FROM 'LDAP://fsc.net' WHERE sAMAccountName='" & strComputerName & "' AND objectClass = 'computer'"
Set oRS = oConnection.Execute(strQ)

msgbox oRS.RecordCount

Do Until oRS.EOF 'loop throuugh computers
For i = 0 To oRS.Fields.Count - 1
msgbox oRS.Fields(i).Name & ": " & oRS.Fields(i)
strADSPath = oRS.Fields(i)
Next
oRS.MoveNext
Loop

Set objComputer = GetObject(strADSPath)
msgbox objComputer.cn

objComputer.some_property = "somevalue"
objComputer.SetInfo

Set objComputer = Nothing
'close other objects as well
 
the last post does work i have just tried it, well i should say it returns the computers adspath and then after the set - getobject it returns the computers CN....slick. the first post should work as well but it is no where near as much use if you want to act/get lost of objects and act on them,,,,powerful though so caution!!!!

one thing about the SQL quiery and the SAMAccount name i have used,,,you have to put a $ on the end of the usual normal netbios name.

have fun
mistamovie
 
On the above post, i couldnt leave it without saying the following. i would attack the issue from the other end.

lets say you have 8000 seats, every morning you will get 8000 users login on and on top of that you will get 8000 attempts to write back to AD. LDAP is pretty slick when it comes to reading data from it but writing back is another matter.

i would do the following. query AD with something like

strQ = "SELECT * FROM 'LDAP://fsc.net' WHERE cn='A*' AND objectClass = 'computer' AND prop_Model = ''"

then you have a recordset of all the machines that start with an A and that dont have the model property filled in.

you can then loop through the recordset of machine names and use DCOM ...Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
as you had before to connect to each machine in turn and then update AD. this way you are in control of how many updates get written to AD. you can run this query once a week, that way you wont get 8000 writes to AD each day.

ok you could still run the script on each machine at logon but you could check AD first to see if the fields were filled out and then only write if you needed to.....

the choice as bruce-eee said is yours.
regards
mistamovie
 
Cheers Dude,

Ill give this a look at on monday morning

Cheers again

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top