<?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>108.bz &#187; LDAP</title>
	<atom:link href="http://www.108.bz/posts/tag/ldap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.108.bz</link>
	<description>Wandering futilities...</description>
	<lastBuildDate>Fri, 27 May 2011 09:08:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Bulk unlocking Active Directory user accounts</title>
		<link>http://www.108.bz/posts/it/bulk-unlocking-active-directory-user-accounts/</link>
		<comments>http://www.108.bz/posts/it/bulk-unlocking-active-directory-user-accounts/#comments</comments>
		<pubDate>Sat, 15 May 2010 22:23:59 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=475</guid>
		<description><![CDATA[This post will show you how to generate a list of all the users&#8217; Distinguished Name, then filter it, then do something useful with it. Scenario: saturday morning (after having crashed into bed at 4:00 a.m., btw), Customer calls. A virus hit the Company and one of the most annoying consequences of the outburst, is [...]]]></description>
			<content:encoded><![CDATA[<p>This post will show you how to generate a list of all the users&#8217; <a href="http://en.wikipedia.org/wiki/Distinguished_Name">Distinguished Name</a>, then filter it, then do something useful with it.</p>
<p>Scenario: saturday morning (after having crashed into bed at 4:00 a.m., btw), Customer calls. A virus hit the Company and one of the most annoying consequences of the outburst, is that every domain user account gets locked due to brute-force login attempts (as per the &#8220;Account Lockout Threshold&#8221; <a href="http://technet.microsoft.com/en-us/library/dd277400.aspx">policy</a>). While they run around cleaning PCs and fixing A/V installations<sup class='footnote'><a href='#fn-475-1' id='fnref-475-1'>1</a></sup>, I&#8217;m asked for a method to quickly unlock the accounts.</p>
<p>I tend to carry out these kind of tasks &#8220;the Unix way&#8221;, using the available DOS prompt commands and a bit of VBScript.</p>
<ul>
<li>Start off by calling LDIFDE:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ldifde -r &quot;(objectclass=user)&quot; -l sAMAccountName -m -f users.ldf</div></div>
<p><a href="http://support.microsoft.com/kb/237677">LDIFDE</a> exports/imports Active Directory data to/from properly formatted (<a href="http://en.wikipedia.org/wiki/LDIF">LDIF</a>) text files. I use it a lot. Ran as shown above, LDIFDE exports the objects of class &#8220;user&#8221; into a file named <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">users.ldf</span> . Of the many attributes an LDAP object bears, I tell LDIFDE to output just the &#8220;sAMAccountName&#8221; one. If I hadn&#8217;t specified any attribute, in the resulting file I&#8217;d have found duplicate DNs for the same user. That&#8217;s because of how the resulting LDIF file is described. Some A/D data is &#8220;incrementally&#8221; added to existing objects given their DN. I just picked sAMAccountName because every user has one and, also, to keep the file small.</li>
<li>Then:
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">findstr /I /b dn.*ou=service.users users.ldf &gt; service_users.txt<br />
findstr /I /b dn.*cn=users users.ldf &gt; normal_users.txt</div></div>
<p><a href="http://technet.microsoft.com/en-us/library/bb490907.aspx">findstr</a> is Microsoft&#8217;s &#8220;poor man version&#8221; of <a href="http://en.wikipedia.org/wiki/Grep">grep</a>, supporting a subset of the regular expression everyone has or should&#8217;ve come to love. Here I&#8217;m using it to extract Distinguished Names from the LDIF (only the ones that lie in a given Organizational Unit), and saving them to the <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">*_users.txt</span> files. They will look like:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dn: CN=squidauth,OU=Service Users,DC=contoso,DC=com<br />
dn: CN=exchangebackup,OU=Service Users,DC=contoso,DC=com<br />
dn: CN=ldap,OU=Service Users,DC=contoso,DC=com<br />
dn: CN=batchcopy,OU=Service Users,DC=contoso,DC=com</div></div>
</li>
<li>Here&#8217;s the VBScript function to unlock an account given its DN:
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Sub</span> unlockuser(userDN)<br />
&nbsp; <span style="color: #000080;">Set</span> objUser = GetObject (<span style="color: #800000;">&quot;LDAP://&quot;</span> &amp; userDN)<br />
&nbsp; objUser.IsAccountLocked = <span style="color: #000080;">False</span><br />
&nbsp; objUser.SetInfo<br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>We just need to transform findstr&#8217;s output, substituting the leading &#8220;<span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">dn: </span>&#8221; with &#8220;<span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">unlockuser</span>&#8221; and enclosing in double quotes what follows. At the top of the new, transformed, file, we&#8217;ll copy/paste <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">unlockuser</span> subroutine definition. That&#8217;ll make our final script.</li>
<li>How to carry out the transform? Using this VBS snippet; it processes its <a href="http://en.wikipedia.org/wiki/Standard_input">Standard Input</a> line by line, and outputs the modifications on Standard Output, just like any Unix file filtering command.
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Set</span> StdIn = WScript.StdIn <br />
<span style="color: #000080;">Do</span> <span style="color: #000080;">While</span> <span style="color: #000080;">Not</span> StdIn.AtEndOfStream <br />
&nbsp; &nbsp; <span style="color: #000080;">line</span> = stdin.readline<br />
&nbsp; &nbsp; <span style="color: #000080;">line</span> = right(<span style="color: #000080;">line</span>,len(<span style="color: #000080;">line</span>)-4)<br />
&nbsp; &nbsp; wscript.echo <span style="color: #800000;">&quot;unlockuser &quot;</span><span style="color: #800000;">&quot;&quot;</span> &amp; <span style="color: #000080;">line</span> &amp; <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot;&quot;</span><br />
<span style="color: #000080;">Loop</span></div></div>
<p>I saved it in a &#8220;<span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">dnfilter.vbs</span>&#8221; file and used it this way:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">type service_users.txt | cscript /nologo dnfilter.vbs &gt; unlock_service_users.vbs</div></div>
<p>To obtain something like this:</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">unlockuser <span style="color: #800000;">&quot;CN=squidauth,OU=Service Users,DC=contoso,DC=com&quot;</span><br />
unlockuser <span style="color: #800000;">&quot;CN=exchangebackup,OU=Service Users,DC=contoso,DC=com&quot;</span><br />
unlockuser <span style="color: #800000;">&quot;CN=ldap,OU=Service Users,DC=contoso,DC=com&quot;</span><br />
unlockuser <span style="color: #800000;">&quot;CN=batchcopy,OU=Service Users,DC=contoso,DC=com&quot;</span></div></div>
</li>
</ul>
<p>As I said, add the <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">unlockuser</span> function at the top of <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">unlock_service_users.vbs</span> and you&#8217;ll have your bulk unlocking script.</p>
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-475-1'>A/V usefulness is often questionable. At least three times a year an unfortunate Customer gets infected by a 0-day threat&#8230; <img src='http://www.108.bz/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  <span class='footnotereverse'><a href='#fnref-475-1'>&#8617;</a></span></li>
</ol>
</div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=475" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/bulk-unlocking-active-directory-user-accounts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

