Get-NestedMembers of an AD group

Here is a script I did for marketing. It pulls a mail group from AD
and outputs the First Name, Last Name and EMail address of all the members
of the group. Thanks to the PowerShell Guy for the function.
#<------- POSH Script ----------------------------------->
function get-NestedMembers ($group){ 
  if ($group.objectclass[1] -eq 'group') { 
        write-verbose 'Group $($group.cn)' 
    $Group.member |% { 
      $de = new-object directoryservices.directoryentry('LDAP://$_') 
      if ($de.objectclass[1] -eq 'group') { 
        get-NestedMembers $de 
      } 
      Else {       
        $ws.cells.item($row,1) = $de.givenname.tostring()
     $ws.Cells.Item($row,2) = $de.SN.tostring()
     $ws.Cells.Item($row,3) = $de.mail.tostring()
     $row++
      } 
    } 
  }
  Else {
    Throw '$group is not a group'
  } 
}$xlSummaryAbove = 0
$xlSortValues = $xlPinYin = 1
$xlAscending = 1
$xlDescending = 2
$xl = New-Object -comobject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$row = 2
$group = New-Object directoryservices.directoryentry('LDAP://cn=MailGroup,OU=Groups,DC=mow,DC=Local')
             $N = $group.Name.tostring()
             $N = $N.Substring(0,29)
             $ws.name = $N.tostring()
$ws.Cells.Item(1,1) = 'First Name'
$ws.Cells.Item(1,2) = 'Last Name'
$ws.Cells.Item(1,3) = 'EMail Address'
$range = $ws.range('A1:C1')
$range.font.bold = 'true'
get-NestedMembers $group
# one-column sort --> works
$range1 = $ws.range('A2:C250')
$range2 = $ws.range('B2') # Sort by last name
[void]$range1.sort($range2, 1,,,,,,,1)
$range1.entireColumn.Autofit()
#<----------- end script -------------------------------->

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s