Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


May 05, 2009

Active Directory Growth Tracker: A Script to Count Objects

Keep an eye on specific AD areas for planning and forecasting
RSS
Subscribe to Windows IT Pro | See More Active Directory (AD) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

LISTING 1: ADacctCountsToXL.vbs

 

On Error Resume Next

******* Begin Callout A *******

DBPath = "C:\scripts\ADacctTrack\"

******* End Callout A *******

AccountCountDB = DBPath & "ADAccountCounts.xml"

Set fso = CreateObject("Scripting.FileSystemObject")	

If fso.FileExists(AccountCountDB) Then
 Set DRS = CreateObject("ADODB.Recordset")
 DRS.Open AccountCountDB

******* Begin Callout B *******

DRS.Sort = "RunDate ASC"

******* End Callout B *******

Else
 Set fso = Nothing
 strMessage = AccountCountDB & " Not Found...Terminating Script!"
 strScriptName = "AD Account Counts"
 CreateObject("WScript.Shell").Popup strMessage,10,strScriptName,vbInformation
 Wscript.Quit
End If

Set XL = CreateObject("Excel.Application")
XL.Workbooks.Add
XL.Sheets.Add.name = "AccountCounts"
XL.Sheets("AccountCounts").Select
XL.Visible = TRUE

******* Begin Callout C *******

Set FldRef = CreateObject("Scripting.Dictionary")

Set objFields = DRS.Fields
'*** Use number of fields to set array dimension
'*** reduce number by 2.  One to account for zero based array
'*** and another to omit 'RunDate' field
FldDim = objFields.count - 2
 
Dim DRSFields()
Redim Preserve DRSFields(flddim)
incr = 0
For Each objField In objFields
'*** This block of code sets up a Field/Row association
'*** A specific field will have a specific Row in the Excel spreadsheet
'*** 'Rundate' is the first field in the database and is not
'*** used as a Row so it is ignored.  Fields start on Row 2
'*** Rundate dates start in Col 2.  This format is good for charts
 If Lcase(objField.Name) <> "rundate" Then
  FldRef.Add objField.Name,incr+2     'Field name and Row assignment
  DRSFields(incr) = objField.Name
  incr = incr + 1
 End If
Next

******* End Callout C *******

******* Begin Callout D *******

'*** Fill Column A with Fieldnames
For i = 0 to Ubound(DRSFields)
 XL.Cells(i+2,1).Value = DRSFields(i) 'start at row 2
Next

******* End Callout D *******

Col = 2

******* Begin Callout E *******

DRS.MoveFirst
Do while Not DRS.EOF
 StoreDate = DRS.Fields.Item("RunDate")
 XL.Cells(1,Col).Value = Cstr(DRS.Fields.Item("RunDate"))
 Do While StoreDate = DRS.Fields.Item("RunDate")
  For i = 0 to Ubound(DRSFields)
   If FldRef.Exists(DRSFields(i)) Then
    '*** find associated Field/Row
    Row = FldRef.item(DRSFields(i))
    XL.Cells(Row,Col).Value = Cdbl(DRS.Fields.Item(DRSFields(i)))
   End If
  Next
  DRS.MoveNext
  If DRS.EOF Then
   Exit Do
  End If
 Loop
 Col = Col + 1  'put next rundate in next column
Loop

******* End Callout E *******

DRS.Close
Set fso = nothing
Set DRS = nothing
XL.Cells.EntireColumn.AutoFit
XL.Range("A1").Select

strMessage = "Done"
strScriptName = "AD Account Counts"
CreateObject("WScript.Shell").Popup strMessage,15,strScriptName,vbInformation

End of Article

   Previous  1  [2]  Next  


Reader Comments
Unable to download the code!

ashers2008 May 08, 2009 (Article Rating: )


My bad.
It should work now. Comment here again if it doesn't. And thanks!
Caroline Marwitz
Editor

Caroline from editorial May 08, 2009 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Meeting Compliance Objectives in SharePoint

Email Controls and Regulatory Compliance

Related Events Troubleshooting Active Directory

Concrete Ways to Make Sure Your SharePoint Deployment Doesn't Blow Up

PCI Requirements for Windows and Active Directory: Straight from a Certified Auditor

Check out our list of Free Email Newsletters!

Active Directory (AD) eBooks The Essentials Series: Active Directory 2008 Operations

Keeping Your Business Safe from Attack: Monitoring and Managing Your Network Security

Windows 2003: Active Directory Administration Essentials

Related Active Directory (AD) Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement