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

AD VB Script Query 1

Status
Not open for further replies.

wotsit

MIS
Oct 18, 2002
47
0
0
GB
Hi,

I'm new to scripting and I'm a bit stuck, so please forgive me. I've been searching around the internet, but have got myself even more confused.

Is there a way to search the local AD and list all of the objects that have a class of computers and then put that information along with what is in their description field and then export it to a .csv or .txt file? Both of these fields are on the object tab of the AD object properties screen.

I have only created scripts querying wmi related things before and I don't know where to start.

Does anybody know of any useful websites that explain how to script using AD properties?

Thank you for any help,
H

 
You can do it with VBScript, but if you need a simple query run that will export to CSV format, then use the CSVDE command.

Code:
csvde -f datadump.csv -r "(&(objectClass=computer))" -l "description"

If you need more complex manipulation, then we can discuss a VB solution.

PSC
[—] CCNP[sub][blue]x3[/blue][/sub] (Security/R&S/Wireless) [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --from "Hackers
 
Well, you are probably hitting a wall because the information you want is not all stored in the same place. Description is not stored in Active Directory.

Here is a script that will do exactly what you requested. It will give you a text file (tab delimited). BE PATIENT, it will have to connect to each PC to get the description. When completed the script will say "DONE" so don't try and open the report until you see that message.

Code:
On Error Resume Next
const HKEY_LOCAL_MACHINE = &H80000002
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("ComputerReport.txt",2,True)

Set oRootDSE = GetObject("LDAP://rootDSE")
strDomain = oRootDSE.get("defaultNamingContext")
qQuery = "<LDAP://" & strDomain & ">;" & _
		"(objectCategory=Computer)" & _
       ";distinguishedName,name;subtree"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = qQuery
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
    PCName = objRecordSet.Fields("name")
	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
 	PCName & "\root\default:StdRegProv")
	strKeyPath = "System\CurrentControlSet\Services\lanmanserver\parameters"
	strValueName = "srvcomment"
	oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
    ts.writeline PCName & vbTab & strValue
	WScript.Sleep 3000
	strValue = Nothing
    objrecordset.MoveNext
Wend
MsgBox "DONE"
objConnection.Close

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top