Sending Active Directory Machine Information To Excel

 

This Vbs script will search the domain specified and list the machines name, DNS host name, and operating system version and service pack level to Excel

 

Note: Be sure to change Your_DC_Name to the DC that you want to query on the line that reads:

 

strLDAPQuery = "LDAP://DC=Your_DC_Name,DC=Com"

 

 

Vbs Script:

 

On Error GoTo 0

Const ADS_SCOPE_SUBTREE = 2

 

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand =   CreateObject("ADODB.Command")

objConnection.Provider = "ADSDSOObject"

objConnection.Open "Active Directory Provider"

 

strLDAPQuery = "LDAP://DC=Your_DC_Name,DC=Com"

strScriptPath = replace(wscript.scriptfullname,wscript.scriptname,"")

 

Set objXL = wscript.CreateObject("Excel.Application")

objXL.Visible = True

objXL.WorkBooks.Add

 

outputFile = strScriptPath & "Computer Accounts.xls"

icount = 2

 

Set objCOmmand.ActiveConnection = objConnection

 

objCommand.CommandText = "Select Name, dnsHostName, operatingSystem, operatingSystemServicePack from '" _

& strLDAPQuery &  "' Where objectClass='computer'" 

 

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRecordSet = objCommand.Execute

 

objRecordSet.MoveFirst

objXL.ActiveSheet.Range("A1:D1").ColumnWidth = 20

ObjXL.ActiveSheet.Cells(1,1).Value = "Machine Name"

ObjXL.ActiveSheet.Cells(1,2).Value = "Dns Host Name"

ObjXL.ActiveSheet.Cells(1,3).Value = "Operating System Version"

ObjXL.ActiveSheet.Cells(1,4).Value = "Service Pack"

 

objXL.Range("A1:E1").Select

objXL.Selection.Font.Bold = True

objXL.Selection.Interior.ColorIndex = 1

objXL.Selection.Interior.Pattern = 1 'xlSolid

objXL.Selection.Font.ColorIndex = 2    

 

Do Until objRecordSet.EOF

On Error GoTo 0

ObjXL.ActiveSheet.Cells(icount,1).Value = objRecordSet.Fields("Name").Value

ObjXL.ActiveSheet.Cells(icount,2).Value = objRecordSet.Fields("DnsHostName").Value

ObjXL.ActiveSheet.Cells(icount,3).Value = objRecordSet.Fields("OperatingSystem").Value

ObjXL.ActiveSheet.Cells(icount,4).Value = objRecordSet.Fields("operatingSystemServicePack").Value

icount = icount + 1

 

objRecordSet.MoveNext

Loop

 

Published Sunday, February 25, 2007 10:13 AM by dhite
Filed under:

Comments

No Comments