<?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>vmweaver.com &#187; Group Membership</title>
	<atom:link href="http://vmweaver.com/index.php/tag/group-membership/feed/" rel="self" type="application/rss+xml" />
	<link>http://vmweaver.com</link>
	<description>Mindless ramblings of a geek...</description>
	<lastBuildDate>Thu, 06 Oct 2011 20:42:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Powershell &#8211; Recursive Group Membership</title>
		<link>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/</link>
		<comments>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 02:11:57 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Group Membership]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=146</guid>
		<description><![CDATA[Well, I am back for yet another Powershell script. This is one that I found pretty useful actually. As one of the people really pushing automation in my group at work, I was tasked with getting a list of all users in the &#8220;Domain Admins&#8221; group for all domains in our Active Directory forest. One [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I am back for yet another Powershell script. This is one that I found pretty useful actually.<br />
As one of the people really pushing automation in my group at work, I was tasked with getting a list of all users in the &#8220;Domain Admins&#8221; group for all domains in our Active Directory forest.</p>
<p>One of the challenges in doing this is that you may have a bunch of nested groups and we needed to dump users from all nested groups, etc.  I do realize that there are probably tools and what-not that would this for me, but what fun is that and why spend the bucks if you can script it out.  I like this approach, too, because I can force the output to be whatever I want and in whichever format is best for what I am trying to accomplish.</p>
<p>From my days in college as a computer science kinda guy, I figured we could use recursion to help walk us through all nested groups.</p>
<p>So for those of you unfamilar with this idea of recursion, I will summarize:<br />
It is basically a function that calls itself until a certain condition is met. At this point the function exits. I know all you CS types may take exception to such a simplified definition, so please google for it or hit up Wikipedia for more detailed info on recursion.</p>
<p>How can this possible help us in our quest to enumeration group memberships?  Well let&#8217;s break it down a little.</p>
<ol>
<li>I start with a group I care about. Let&#8217;s say it is &#8220;Domain Admins&#8221; for domain &#8220;office1.contoso.com&#8221;.</li>
<li>I have a function (&#8220;get-groupmembers&#8221;) that I use to enumerate the members of this group and do something with them (output, write to file, etc)</li>
<li>As I am enumerating them, I find a member that is of type &#8220;group&#8221; called something like &#8220;Corp Admins&#8221;</li>
<li>I now call my function (&#8220;get-groupmembers&#8221;) with this nested group (&#8220;Corp Admins&#8221;) to enumerate the members</li>
<li>As I am enumerating them, I find ANOTHER group called &#8220;Help Desk On-Call&#8221; inside of &#8220;Corp Admins&#8221;</li>
<li>I can now call my funciton (&#8220;get-groupmembers&#8221;) ANOTHER time and keep going until I only have users and have walked all of the nested groups</li>
</ol>
<p>I know this may sound a little weird &#8220;that I am calling myself&#8221; over and over again, but it is actually pretty efficient.</p>
<p>Let&#8217;s jump into some code now.</p>
<p>For starters, I need to have some functions that take a Fully Qualified Domain Name (for an Active Directory Domain) and convert it into an LDAP-ish format.  For example, I needed &#8220;office1.contoso.com&#8221; to be transformed into &#8220;DC=office1,DC=contoso,DC=com&#8221;.  I know this isn&#8217;t rocket-surgery, but I just threw some stuff together for it.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Convert<span style="color: pink;">-</span>DNStoDN <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$DNSName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008000;">#  Create an array of each item in the string separated by &quot;.&quot;</span>
   <span style="color: #800080;">$DNSArray</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DNSName</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;.&quot;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #008000;"># Let's go through our new array and do something with each item</span>
   <span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$x</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$DNSArray</span>.Length ; <span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #008000;">#I don't want a comma after my last item, so check to see if I am on my last one and set</span>
        <span style="color: #008000;"># $Separator equal to nothing.</span>
        <span style="color: #008000;"># Remember that we need to go to Length-1 because arrays are &quot;0 based indexes&quot;</span>
         <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNSArray</span>.Length <span style="color: pink;">-</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #0000FF;">else</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span><span style="color: #800000;">&quot;,&quot;</span><span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$DN</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot;DC=&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$DNSArray</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$x</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Separator</span>
      <span style="color: #000000;">&#125;</span>
   <span style="color: #0000FF;">return</span> <span style="color: #800080;">$DN</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We will also need to be able to split the FQDN of the DOMAIN out from the DN of a group or user. So, I have something like &#8220;CN=Me,OU=User1,DC=office1,DC=contoso,DC=com&#8221; and want to get the FQDN of this domain.  For this example this would output &#8220;office1.contoso.com&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Convert<span style="color: pink;">-</span>DNtoDNS <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$DN</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$DNArray</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DN</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #008000;"># Let's go through our new array and do something with each item</span>
   <span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$x</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$DNArray</span>.Length ; <span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #008000;">#I don't want a period after my last item, so check to see if I am on my last one and set</span>
        <span style="color: #008000;"># $Separator equal to nothing.</span>
        <span style="color: #008000;"># Remember that we need to go to Length-1 because arrays are &quot;0 based indexes&quot;</span>
         <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNArray</span>.Length <span style="color: pink;">-</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #0000FF;">else</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span><span style="color: #800000;">&quot;.&quot;</span><span style="color: #000000;">&#125;</span>
        <span style="color: #008000;"># Now we have to see if we look like &quot;DC=&quot;. If it does, we will</span>
        <span style="color: #008000;"># start to construct our DNS name.</span>
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNArray</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$x</span><span style="color: #000000;">&#93;</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;=&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #FF0000;">-ilike</span> <span style="color: #800000;">&quot;DC&quot;</span><span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#123;</span>
               <span style="color: #008000;"># Let's grab the &quot;contoso&quot; side of the &quot;DC=contoso&quot;</span>
              <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$DNS</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$DNArray</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$x</span><span style="color: #000000;">&#93;</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;=&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Separator</span>
           <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span>
   <span style="color: #0000FF;">return</span> <span style="color: #800080;">$DNS</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now that we have those little &#8220;cameo&#8221; functions, we will move on to the more of the meat-and-potatoes of the script.<br />
The next function will be to actually enumerate a group in Active Directory without using the Quest Tools for Active Directory (if you don&#8217;t have those yet, you need to them).</p>
<p>We are actually going to use some .NET calls to get the directory objects.  My colleague <a title="Mike Hays' Blog" href="http://blog.mike-hays.net/" target="_blank">Mike Hays</a> actually did a lot of this part of the code.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> get<span style="color: pink;">-</span>groupmember<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$domain</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$groupName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008000;"># I have passed in the FQDN and Groupname I am interested in</span>
   <span style="color: #008000;"># I just need to convert my FQDN into an LDAP style name using my previous function</span>
   <span style="color: #800080;">$DN</span> <span style="color: pink;">=</span> convert<span style="color: pink;">-</span>DNStoDN<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Domain</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$domainLDAPUrl</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;LDAP://&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$DN</span>
   <span style="color: #008000;"># Setup my directory connection using .NET call</span>
   <span style="color: #800080;">$ent</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.DirectoryEntry<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$domainLDAPUrl</span> <span style="color: #000000;">&#41;</span>
&nbsp;
   <span style="color: #008000;"># Define my &quot;searcher&quot; object to query the directory</span>
   <span style="color: #800080;">$srch</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.DirectorySearcher<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$ent</span> <span style="color: #000000;">&#41;</span>
&nbsp;
   <span style="color: #008000;"># Setup my search criteria.. looking for all Groups with CN=GroupName</span>
   <span style="color: #800080;">$groupNameFilter</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;(&amp;(objectClass=group)(CN=&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$groupName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;))&quot;</span>
   <span style="color: #800080;">$srch</span>.<span style="color: #0000FF;">Filter</span> <span style="color: pink;">=</span> <span style="color: #800080;">$groupNameFilter</span>
&nbsp;
   <span style="color: #008000;"># Now go execute my query to and put the results in $coll</span>
   <span style="color: #800080;">$coll</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.SearchResultCollection<span style="color: #000000;">&#93;</span>      <span style="color: #800080;">$srch</span>.FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
   <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$rs</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$coll</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
       <span style="color: #008000;"># Now get a collection of properties for that object</span>
       <span style="color: #800080;">$resultPropColl</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ResultPropertyCollection<span style="color: #000000;">&#93;</span> <span style="color: #800080;">$rs</span>.Properties
&nbsp;
       <span style="color: #008000;"># Cycle through all group members</span>
       <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$memberColl</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$resultPropColl</span><span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;member&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
           <span style="color: #008000;"># Build my membership array</span>
           <span style="color: #000000;">&#91;</span><span style="color: #008080;">array</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$gpMemberEntry</span> <span style="color: pink;">+=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.DirectoryEntry<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800000;">&quot;LDAP://&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$memberColl</span> <span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
  <span style="color: #008000;"># Send back my group members.</span>
  <span style="color: #0000FF;">return</span> <span style="color: #800080;">$gpMemberEntry</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Okay.. now that we have THAT setup let&#8217;s talk about the next bits of code.<br />
This is where we will have our recursive function &#8220;Get-AllMembers&#8221;.  In it, you will a call to itself.  One of the biggest concerns is that you can end up in an unending or infinite cycle.  I don&#8217;t really do any checking in this little scripty-do-dad, so that may be something for later.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> get<span style="color: pink;">-</span>allmembers<span style="color: #000000;">&#40;</span><span style="color: #800080;">$objectName</span><span style="color: pink;">,</span> <span style="color: #800080;">$OF</span><span style="color: pink;">,</span> <span style="color: #800080;">$GN</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008000;"># Split out my domain name  (should be FQDN) and the group name</span>
    <span style="color: #800080;">$domainName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objectname</span>.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
    <span style="color: #800080;">$ObjectName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objectname</span>.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#93;</span>
&nbsp;
    <span style="color: #800080;">$members</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>groupmember <span style="color: #800000;">&quot;$DomainName&quot;</span> <span style="color: #800000;">&quot;$ObjectName&quot;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$members</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$NULL</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
        <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$member</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$members</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
            <span style="color: #008000;">#  Grab the domain DNS name out of the object DN</span>
            <span style="color: #800080;">$ObjDomain</span> <span style="color: pink;">=</span> convert<span style="color: pink;">-</span>DNtoDNS <span style="color: #800080;">$Member</span>.DistinguishedName
            <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$member</span>.objectclass <span style="color: #FF0000;">-contains</span> <span style="color: #800000;">&quot;group&quot;</span><span style="color: #000000;">&#41;</span>
              <span style="color: #000000;">&#123;</span>
                 <span style="color: #008000;">#If my group member is, itself, a group We get to do some recursion</span>
                  <span style="color: #800080;">$out</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objDomain</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$member</span>.name
                  <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$out</span>
                  <span style="color: #008000;"># Call myself with the nested group name</span>
                  get<span style="color: pink;">-</span>allmembers <span style="color: pink;">-</span>ObjectName <span style="color: #800080;">$out</span> <span style="color: pink;">-</span>OF <span style="color: #800080;">$of</span> <span style="color: pink;">-</span>GN <span style="color: #800080;">$GN</span>
               <span style="color: #000000;">&#125;</span>
            <span style="color: #0000FF;">else</span>
               <span style="color: #000000;">&#123;</span>
                  <span style="color: #008000;"># If I get back a user, then see if the user is disabled or not</span>
                  <span style="color: #800080;">$userAndDomain</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objDomain</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$member</span>.name
                  <span style="color: #008000;"># The  UserAccountControl property contains several &quot;flags&quot;</span>
                  <span style="color: #008000;"># that we can interrogate.  By doing a Binary AND we are seeing if the 2nd flag is set.</span>
                  <span style="color: #000000;">&#91;</span><span style="color: #008080;">bool</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$accountIsDisabled</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$member</span>.userAccountControl.ToSTring<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-band</span> <span style="color: #804000;">2</span>
&nbsp;
                  <span style="color: #008000;"># Setup our output (I am choosing to construct a comma-delimited type of output</span>
                  <span style="color: #800080;">$OutText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;'&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$GN</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$objDomain</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Member</span>.Samaccountname <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Member</span>.displayName <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$member</span>.distinguishedName <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$objectname</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$accountIsDisabled</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;'&quot;</span>
&nbsp;
                  <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #008080; font-style: italic;">-FilePath</span> <span style="color: #800080;">$OF</span> <span style="color: #008080; font-style: italic;">-inputobject</span> <span style="color: #800080;">$Outtext</span> <span style="color: #008080; font-style: italic;">-append</span> <span style="color: #008080; font-style: italic;">-Encoding</span> <span style="color: #800000;">&quot;ASCII&quot;</span>
&nbsp;
                  <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$OutText</span>
                <span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
     <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;No Members or no Group:&quot;</span> <span style="color: #800080;">$ObjectName</span> <span style="color: #800000;">&quot;in Domain:&quot;</span> <span style="color: #800080;">$DomainName</span> <span style="color: pink;">-</span>Foreground RED
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">###########################</span>
<span style="color: #008000;">## Main</span>
<span style="color: #008000;">###########################</span>
<span style="color: #800080;">$GroupName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Domain Admins&quot;</span>
<span style="color: #800080;">$Today</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-format</span> <span style="color: #800000;">&quot;yyyyMMddhh&quot;</span>
<span style="color: #800080;">$OutputFolder</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Temp\&quot;</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800080;">$outputFolder</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$False</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$OutputFolder</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Type</span> Directory <span style="color: pink;">&gt;</span> <span style="color: #800080;">$NULL</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Grab my forest info</span>
<span style="color: #800080;">$forest</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Forest<span style="color: #000000;">&#93;</span>::GetCurrentForest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#Setup my output file</span>
<span style="color: #800080;">$OutHeader</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;'Group','UserDomain','SAMAccount','DisplayName','DN','MemberofGroup','IsDisabled'&quot;</span>
&nbsp;
<span style="color: #008000;"># Define output file name</span>
<span style="color: #800080;">$of</span> <span style="color: pink;">=</span> <span style="color: #800080;">$OutputFolder</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Today</span> <span style="color: pink;">+</span><span style="color: #800000;">&quot;_&quot;</span><span style="color: pink;">+</span> <span style="color: #800080;">$GroupName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;-AuditReport.txt&quot;</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #008080; font-style: italic;">-FilePath</span> <span style="color: #800080;">$OF</span> <span style="color: #008080; font-style: italic;">-inputobject</span> <span style="color: #800080;">$OutHeader</span> <span style="color: #008080; font-style: italic;">-Encoding</span> <span style="color: #800000;">&quot;ASCII&quot;</span>
&nbsp;
<span style="color: #008000;"># Cycle through all the child domains in the forest root to query for Group.</span>
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$domain</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$forest</span>.Domains<span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$FullGroupName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$domain</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$GroupName</span>
    get<span style="color: pink;">-</span>allmembers <span style="color: pink;">-</span>ObjectName <span style="color: #800080;">$FullGroupName</span> <span style="color: pink;">-</span>OF <span style="color: #800080;">$of</span> <span style="color: pink;">-</span>GN <span style="color: #800080;">$FullGroupName</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Just take all of the script blocks from above and paste them into your script.  I am trying to keep these posts a bit shorter, so you may see upcoming posts broken out into parts.</p>
<p>Well, I think I am done here with this one.  Please let me know if you have questions, concerns, or comments.</p>
<p>Please keep in mind that this script will attempt to enumerate the Group in  ALL child domains in your current AD Forest.  If you have a large Forest with lots of child domains&#8230;..this could take a while.</p>
<p>I will be happy to help out with requested changes if they seem like they would be beneficial overall, but I am also a STRONG advocate of doing-it-yourself.<br />
Every bit of Powershell and scripting I have learned by grabbing it and going with it.</p>
<p>Anyway, as always&#8230;thanks for stopping by and happy scripting!!!</p>
<p>&#8211; Mark</p>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

