<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Monie Studios &#187; ad-report</title>
	<atom:link href="http://www.moniestudios.com/tag/ad-report/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.moniestudios.com</link>
	<description>So much to learn, so little time!</description>
	<lastBuildDate>Sat, 21 Aug 2010 04:59:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How to Get Active Directory Accounts Information Using VB Script</title>
		<link>http://www.moniestudios.com/tutorials/ad-report/</link>
		<comments>http://www.moniestudios.com/tutorials/ad-report/#comments</comments>
		<pubDate>Fri, 08 May 2009 00:10:46 +0000</pubDate>
		<dc:creator>Monie</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[active directory]]></category>
		<category><![CDATA[ad-report]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.moniestudios.com/?p=301</guid>
		<description><![CDATA[Learn how to generate a text based report for your Active Directory with the help of VB Script.]]></description>
			<content:encoded><![CDATA[<p>Window Server 2003/2008 Active Directory has lacks tool to run reports on things like current User Accounts, Disable Accounts, Organizational Units etc. There are many tools out there like <a href="http://www.manageengine.com/products/ad-manager/index.html">ADManager Plus</a> or <a href="http://maxpowersoft.com/">AD Reports</a> that will do that, but most of them are limited for free use only.</p>
<p>So how do you get your hands on this report for free?</p>
<h2>The Magic Script</h2>
<p style="clear: both;">If you know Visual Basic Scripting (VBS), you can pull out any report from any Active Directory object really easy.</p>
<p>As shown below, this VB Script will generate a list of information about the user Username, First Name, Last Name and Email Address. This list will then be exported to Microsoft Excel as comma separated value (CSV) file.</p>
<pre class="brush: vb;">
&lt;%
Option Explicit

' Generate an excel csv file with user.csv file name.
Const REPORT_FILE = &quot;users.csv&quot;

Const ADS_SCOPE_SUBTREE = 2
Const ADS_UF_ACCOUNTDISABLE = 2

Dim objFileSystem, objFile, objConnection, objCommand, objRootDSE, objRecordSet
Dim strUsername, strFirstname, strLastname, strEmail
Dim intUAC

Set objFileSystem = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objFile = objFileSystem.OpenTextFile(REPORT_FILE, 2, True, 0)

objFile.WriteLine &quot;AD Username,First Name,Last Name,E-mail,Company&quot;

Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
objConnection.Provider = &quot;ADsDSOObject&quot;
objConnection.Open &quot;Active Directory Provider&quot;

Set objCommand = CreateObject(&quot;ADODB.Command&quot;)
objCommand.ActiveConnection = objConnection

Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
objCommand.CommandText = &quot;SELECT sAMAccountName, userAccountControl, givenName, sn, mail, company &quot; &amp;_
  &quot;FROM 'LDAP://&quot; &amp; objRootDSE.Get(&quot;defaultNamingContext&quot;) &amp;_
  &quot;' WHERE objectClass='user' AND objectCategory='person'&quot;
Set objRootDSE = Nothing

objCommand.Properties(&quot;Page Size&quot;) = 1000
objCommand.Properties(&quot;Timeout&quot;) = 600
objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE
objCommand.Properties(&quot;Cache Results&quot;) = False
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF

  intUAC = objRecordSet.Fields(&quot;userAccountControl&quot;)

  If intUAC And ADS_UF_ACCOUNTDISABLE Then
  ' Account is Disabled, ignore it.
  Else
    On Error Resume Next
    strUsername = &quot;&quot; : strUsername = objRecordSet.Fields(&quot;sAMAccountName&quot;)
    strFirstname = &quot;&quot; : strFirstname = objRecordSet.Fields(&quot;givenName&quot;)
    strLastname = &quot;&quot; : strLastname = objRecordSet.Fields(&quot;sn&quot;)
    strEmail = &quot;&quot; : strEmail = objRecordSet.Fields(&quot;mail&quot;)
    strCompany = &quot;&quot; : strEmail = objRecordSet.Fields(&quot;company&quot;)
    On Error Goto 0

    objFile.WriteLine strUsername &amp; &quot;,&quot; &amp; strFirstname &amp; &quot;,&quot; &amp; strLastname &amp; &quot;,&quot; &amp; strEmail &amp; &quot;,&quot; &amp; strCompany
  End If

  objRecordSet.MoveNext
Wend

objConnection.Close
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
%&gt;
</pre>
<h2>Script Execution</h2>
<p>Just copy and paste this code to your notepad or any text editor you have, and save it as with .vbs extension. To run them, just double click on the file and it will automatically generate a csv file in the same root directory from where you save the .vbs file.</p>
<p>Here is the sample image on how your report will look like:<br />
<img class="alignnone size-full wp-image-305" title="ad_report" src="http://www.moniestudios.com/wp-content/uploads/ad_report.jpg" alt="ad_report" width="608" height="250" /></p>
<h2>Extra Information</h2>
<p>Of course, that is not the only information that you can get from the Active Directory. Below are some of the field name that you can query out to be included in your csv file.</p>
<p>Add this to code line #26 and add in the appropriate information in code line #45 and you are done. But before that, make sure you give this extra information a title in code line #16.</p>
<ul>
<li>Username: <strong>sAMAccountName</strong></li>
<li>First name: <strong>givenname</strong></li>
<li>Initials: <strong>middleName</strong></li>
<li>Last name: <strong>sn</strong></li>
<li>First and Last name: <strong>displayName</strong></li>
<li>Description: <strong>description</strong></li>
<li>Address: <strong>physicaldeliveryofficename</strong></li>
<li>Telephone: <strong>telephonenumber</strong></li>
<li>Mail: <strong>mail</strong></li>
<li>Homepage: <strong>wwwhomepage</strong></li>
<li>Street: <strong>streetAddress</strong></li>
<li>vPostofficebox: <strong>postofficebox</strong></li>
<li>City: <strong>l (that is L lowercase)</strong></li>
<li>State: <strong>st</strong></li>
<li>Postal code: <strong>postalcode</strong></li>
<li>Country: <strong>co</strong></li>
<li>Home phone: <strong>homephone</strong></li>
<li>Pager No.: <strong>pager</strong></li>
<li>Mobile phone No.: <strong>mobile</strong></li>
<li>Fax No.: <strong>facsimiletelephonenumber</strong></li>
<li>IP Phone: <strong>ipphone</strong></li>
<li>Remarks: <strong>info</strong></li>
<li>Title: <strong>title</strong></li>
<li>Department: <strong>department</strong></li>
<li>Company: <strong>company</strong></li>
<li>Manager: <strong>manager</strong></li>
</ul>
<p>To discover more LDAP attributes, go to start > run and typed in this command:<br />
<strong>CSVDE -f Exportfile.csv</strong>. Then open Exportfile.csv with Microsoft Excel application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moniestudios.com/tutorials/ad-report/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
