Script Center >
Scripting Forums
>
The Official Scripting Guys Forum!
>
Retrieve computer list from AD
Retrieve computer list from AD
- I need to retrieve a list of computers from AD then interogate the computers to retrieve the logged on user and the computer description. Preferably using vbs
Answers
- Try this:
1 Const ADS_SCOPE_SUBTREE = 2 2 Set objConnection = CreateObject("ADODB.Connection") 3 Set objCommand = CreateObject("ADODB.Command") 4 objConnection.Provider = "ADsDSOObject" 5 objConnection.Open "Active Directory Provider" 6 Set objCOmmand.ActiveConnection = objConnection 7 objCommand.CommandText = _ 8 "Select Name, Description from 'LDAP://DC=fabrikam,DC=com' " _ 9 & "where objectClass='computer'" 10 objCommand.Properties("Page Size") = 1000 11 objCommand.Properties("Timeout") = 30 12 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 13 objCommand.Properties("Cache Results") = False 14 Set objRecordSet = objCommand.Execute 15 objRecordSet.MoveFirst 16 Do Until objRecordSet.EOF 17 ComputerName = objRecordSet.Fields("Name").Value 18 Wscript.Echo "Computer Name: " & ComputerName 19 Wscript.Echo "Description: " & objRecordSet.Fields("Description").Value 20 LoginSessions(ComputerName) 21 objRecordSet.MoveNext 22 Loop 23 24 Sub LoginSessions(strComputer) 25 Set objWMIService = GetObject("winmgmts:" _ 26 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 27 Set colComputer = objWMIService.ExecQuery _ 28 ("Select * from Win32_ComputerSystem") 29 Wscript.Echo "Current User(s) : " 30 For Each objComputer in colComputer 31 Wscript.Echo objComputer.UserName 32 Next 33 End Sub 34
Regards,
Salvador Manaois III
MCSE MCSA CEH MCITP | Enterprise/Server Admin
Bytes & Badz : http://badzmanaois.blogspot.com- Marked As Answer byEd Hayward Monday, November 10, 2008 12:30 AM
All Replies
- Try this:
1 Const ADS_SCOPE_SUBTREE = 2 2 Set objConnection = CreateObject("ADODB.Connection") 3 Set objCommand = CreateObject("ADODB.Command") 4 objConnection.Provider = "ADsDSOObject" 5 objConnection.Open "Active Directory Provider" 6 Set objCOmmand.ActiveConnection = objConnection 7 objCommand.CommandText = _ 8 "Select Name, Description from 'LDAP://DC=fabrikam,DC=com' " _ 9 & "where objectClass='computer'" 10 objCommand.Properties("Page Size") = 1000 11 objCommand.Properties("Timeout") = 30 12 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 13 objCommand.Properties("Cache Results") = False 14 Set objRecordSet = objCommand.Execute 15 objRecordSet.MoveFirst 16 Do Until objRecordSet.EOF 17 ComputerName = objRecordSet.Fields("Name").Value 18 Wscript.Echo "Computer Name: " & ComputerName 19 Wscript.Echo "Description: " & objRecordSet.Fields("Description").Value 20 LoginSessions(ComputerName) 21 objRecordSet.MoveNext 22 Loop 23 24 Sub LoginSessions(strComputer) 25 Set objWMIService = GetObject("winmgmts:" _ 26 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 27 Set colComputer = objWMIService.ExecQuery _ 28 ("Select * from Win32_ComputerSystem") 29 Wscript.Echo "Current User(s) : " 30 For Each objComputer in colComputer 31 Wscript.Echo objComputer.UserName 32 Next 33 End Sub 34
Regards,
Salvador Manaois III
MCSE MCSA CEH MCITP | Enterprise/Server Admin
Bytes & Badz : http://badzmanaois.blogspot.com- Marked As Answer byEd Hayward Monday, November 10, 2008 12:30 AM
- Thank You Salvador
This is just what I was looking for, except maybe that I intended to interrogate the computer itself for the description. The one in My Computer - Computer Name.
Don't get me wrong I am not ungrateful, you have given me a good place to start.
Do you know of a good place to find information on the objects, their methods and properties that are pertinent to vbs?
Thank you onece again
Ed - You can try the MSDN and Script Center for stuff about methods and properties.
Here's a Script Center example of modifying the description of a computer:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec05/hey1207.mspx
Regards,
Salvador Manaois III
MCSE MCSA CEH MCITP | Enterprise/Server Admin
Bytes & Badz : http://badzmanaois.blogspot.com - Hey, just add a line to query the computers description property.
Sub LoginSessions(strComputer) Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") Wscript.Echo "Current User(s) : " For Each objComputer in colComputer Wscript.Echo objComputer.UserName Wscript.Echo objComputer.Description Next End Sub
A good starting place to find the properties of WMI classes is:
http://www.microsoft.com/technet/scriptcenter/tools/wmimatic.mspx
Hope that helps
Cheers :) - Thank you Matthew and Salvadore,
It's funny when something is staring you in the face that you don't try for fear of failure.
objComputer.Description is an obvious guess.
Thank you once again.
Ed - Thank u both, this is really useful, I'm a systems administrator giving my first steps in vb scripting for AD.
would guide me? I need to retrieve this information in files (csv or someting so) where can I search?
Thanks in advance. Gracias desde Madrid