<?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>Vallery.net &#187; active directory</title>
	<atom:link href="http://vallery.net/tag/active-directory/feed/" rel="self" type="application/rss+xml" />
	<link>http://vallery.net</link>
	<description>personal homepage of Jason Vallery</description>
	<lastBuildDate>Sun, 05 Feb 2012 20:13:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using PowerShell and the DirectorySearcher class</title>
		<link>http://vallery.net/2010/04/09/using-powershell-and-the-directorysearched-class/</link>
		<comments>http://vallery.net/2010/04/09/using-powershell-and-the-directorysearched-class/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 22:34:41 +0000</pubDate>
		<dc:creator>jvallery</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[active directory]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://vallery.net/?p=206</guid>
		<description><![CDATA[I needed to locate the LDAP distinguished name of an individual user account in a remote domain via PowerShell.   Assuming your script is running on a box that is part of a domain that has a trust to the remote domain we can do this by running a query against Active Directory with LDAP. By [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to locate the LDAP distinguished name of an individual user account in a remote domain via PowerShell.   Assuming your script is running on a box that is part of a domain that has a trust to the remote domain we can do this by running a query against Active Directory with LDAP.</p>
<p>By using the DirectorySearcher class we can build complex LDAP queries to find objects in Active Directory.   With this information you can do all kinds of fun scripting things.</p>
<p>Here is a sample script:</p>
<pre><span style="color: #000000;">

</span><span style="color: #008000;">#</span><span style="color: #008000;">Specify the search criteria</span><span style="color: #008000;">
</span><span style="color: #800080;">$samname</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">jasonv</span><span style="color: #800000;">"</span><span style="color: #000000;">
</span><span style="color: #800080;">$domain</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">dev.lcl</span><span style="color: #800000;">"</span><span style="color: #000000;">

</span><span style="color: #008000;">#</span><span style="color: #008000;">Get a list of domains in the forest and grab the DN of the one matching the above parameter.</span><span style="color: #008000;">
</span><span style="color: #800080;">$forest</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> [</span><span style="color: #008080;">System.DirectoryServices.ActiveDirectory.Forest</span><span style="color: #000000;">]::</span><span style="color: #8B4513;">GetCurrentForest</span><span style="color: #000000;">()
</span><span style="color: #800080;">$domain</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$forest</span><span style="color: #000000;">.Domains | ? {</span><span style="color: #800080;">$_</span><span style="color: #000000;">.Name </span><span style="color: #FF0000;">-eq</span><span style="color: #000000;"> </span><span style="color: #800080;">$domain</span><span style="color: #000000;">}
</span><span style="color: #800080;">$domainDN</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$domain</span><span style="color: #000000;">.</span><span style="color: #8B4513;">GetDirectoryEntry</span><span style="color: #000000;">().distinguishedName
</span><span style="color: #5F9EA0; font-weight: bold;">Write-Output</span><span style="color: #000000;">  </span><span style="color: #800000;">"</span><span style="color: #800000;">Found the remote domain, the full LDAP distinguished name is $DomainDN</span><span style="color: #800000;">"</span><span style="color: #000000;">

</span><span style="color: #008000;">#</span><span style="color: #008000;">Create an LDAP searcher object and pass in the DN of the domain we wish to query</span><span style="color: #008000;">
</span><span style="color: #800080;">$Searcher</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #5F9EA0; font-weight: bold;">New-Object</span><span style="color: #000000;"> </span><span style="color: #800000;">System.DirectoryServices.DirectorySearcher</span><span style="color: #000000;">([</span><span style="color: #008080;">ADSI</span><span style="color: #000000;">]</span><span style="color: #800000;">"</span><span style="color: #800000;">LDAP://$domainDN</span><span style="color: #800000;">"</span><span style="color: #000000;">)

</span><span style="color: #008000;">#</span><span style="color: #008000;">Pass in the ceriteria we are searching for.</span><span style="color: #008000;">
#</span><span style="color: #008000;">In this case we're looking for users with a particular SAM name.</span><span style="color: #008000;">
</span><span style="color: #800080;">$Searcher</span><span style="color: #000000;">.</span><span style="color: #8B4513;">filter</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">(&amp;(objectCategory=person)(objectClass=user)(sAMAccountName= $samname))</span><span style="color: #800000;">"</span><span style="color: #000000;">
</span><span style="color: #800080;">$results</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$Searcher</span><span style="color: #000000;">.</span><span style="color: #8B4513;">Findall</span><span style="color: #000000;">()

</span><span style="color: #008000;">#</span><span style="color: #008000;">Loop through the results</span><span style="color: #008000;">
</span><span style="color: #0000FF;">Foreach</span><span style="color: #000000;">(</span><span style="color: #800080;">$result</span><span style="color: #000000;"> </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> </span><span style="color: #800080;">$results</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$User</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$result</span><span style="color: #000000;">.</span><span style="color: #8B4513;">GetDirectoryEntry</span><span style="color: #000000;">()
    </span><span style="color: #800080;">$userDN</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;">  </span><span style="color: #800080;">$user</span><span style="color: #000000;">.</span><span style="color: #8B4513;">DistinguishedName</span><span style="color: #000000;">
    </span><span style="color: #5F9EA0; font-weight: bold;">Write-Output</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Found a user matching with the distingused name of $userDN</span><span style="color: #800000;">"</span><span style="color: #000000;">
}

</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://vallery.net/2010/04/09/using-powershell-and-the-directorysearched-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

