header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Trying to export DL Group Members but having problems, easy fix?
Last Post 26 Nov 2011 07:25 AM by Shaba. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Enigma1User is Offline
New Member
New Member
Posts:2
Avatar

--
18 Nov 2011 10:20 AM
    Not sure if the summary makes sense so I'll post more here.

    I have a DL (lets call it 'notifications'), I have been asked to get a breakdown of all users that are a part of 'notifications'. When I run get-distributiongroupmember I get a dozen or two DL's that are members, and then I have to run the same command for those, which in turn have other DL's and so on and so forth.

    I need to be able to break down the entire thing (hopefully in the same excel tab)

    Is this possible to do without having to run the command over and over and also be able to append the same csv?

    Thanks in advance
    Enigma1User is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    18 Nov 2011 04:53 PM
    The below may be a little crude and i'm sure there is an easier way, but this is finally what worked for me in case others have the same issue:
    
    ##Get All recipients of a DL##
    ##############################
    
    ##STUFF YOU FILL OUT##########
    
    $DLName = "DL-groupyouarelookingat"
    $OutputFile = "f:\output\Members_of_$DLName.csv"
    
    ################################
    #DO NOT MODIFY CODE BEYOND HERE#
    ################################
    
    $dlmembers = Get-DistributionGroupMember -Identity $DLName
    foreach($dlmember in $dlmembers){
        $out1 = $dlmember.Name + "," +$dlmember.DisplayName
        $out1 | Out-File $OutputFile -NoClobber -Append -Encoding Default
        
        if($dlmember.RecipientType -like "*Group"){
            $subdlmembers = Get-DistributionGroupMember -Identity $dlmember.Name
            
            foreach($subdlmember in $subdlmembers){
                $out2 = "    " + $subdlmember.Name + "," + $subdlmember.DisplayName
                $out2 | Out-File $OutputFile -NoClobber -Append -Encoding Default
                
                if($subdlmember.RecipientType -like "*Group"){
                    $subdlmembers2 = Get-DistributionGroupMember -Identity $subdlmember.Name
                    
                    foreach($subdlmember2 in $subdlmembers2){
                        $out3 = "        " + $subdlmember2.Name + "," + $subdlmember2.DisplayName
                        $out3 | Out-File $OutputFile -NoClobber -Append -Encoding Default
                        
                        if($subdlmember2.RecipientType -like "*Group"){
                            $subdlmembers3 = Get-DistributionGroupMember -Identity $subdlmember2.Name
                            
                            foreach($subdlmember3 in $subdlmembers3){
                                $out4 = "            " + $subdlmember3.Name + "," + $subdlmember3.DisplayName
                                $out4 | Out-File $OutputFile -NoClobber -Append -Encoding Default
                                
                                if($subdlmember3.RecipientType -like "*Group"){
                                    $subdlmembers4 = Get-DistributionGroupMember -Identity $dlmember.Name
                                    
                                    foreach($subdlmember4 in $subdlmembers4){
                                        $out5 = "                " + $subdlmember4.Name + "," + $subdlmember4.DisplayName
                                        $out5 | Out-File $OutputFile -NoClobber -Append -Encoding Default
                                        
                                    }# end 2nd inside foreach
                                }#end inside if
                            }# end inside foreach
                        }#end outside if
                    }# end 2nd inside foreach
                }#end inside if
            }# end inside foreach
        }#end outside if
    }#end outside foreach
    
    PresenceUser is Offline
    New Member
    New Member
    Posts:94
    Avatar

    --
    18 Nov 2011 06:57 PM
    you could write a recursive function...

    let me post a script that i wrote that counts users in a distribution group (and all distributions lists contained within) and then totals them...

    i wrote this so that our policy of all distribution lists that are over 250 users have to be locked down could be enforced...

    maybe you could work with this? instead of counting the users, you could maybe add them to a table?

    here's the script (Edited sensitive information out of the script)

    ##########################################################
    ## ##
    ## Written by: Presence ##
    ## Section: ##
    ## Office Number: ##
    ## Email Address: ##
    ## ##
    ##########################################################


    #Param ($global:dlname)
    $Time = Get-Date -format "yyyyMMdd_HHmm"
    Start-transcript -Path F:\scripts\DLIST\logs\Distro_Log_$time.wri

    $y = 1
    $global:Count = 0 # Use this as your counter

    ##----------------------------------------------------##
    Function Dlistnumber () {

    If ($global:asdf -lt 1) ##this ensures only the first run through is set to the global variable. so if the function calls itself, it moves onto the next Dlist.
    {
    $Global:asdf = 2
    $dlname1 = $Global:Dlname
    }

    ##write-host "the function was called on:" $dlname "!" ##troubleshooting purposes, to see which list is causing a depth overflow.

    $step2 = get-group -identity $dlname1

    $global:Count += $step2.members.count ##what actually goes through and counts the members...

    $step2.members | Where{$_.Parent -like "OU/distribution groups*"} | export-csv F:\scripts\Dlist\membercounttemp.csv

    $data2 = Import-csv F:\Scripts\Dlist\membercounttemp.csv
    remove-item F:\scripts\dlist\membercounttemp.csv
    If ($data2 -ne $null)
    {
    Foreach ($line in $data2)
    {
    #write-host $global:count
    $global:count -= 1 ##because dlists aren't real people.
    $child = get-group -identity $line.objectguid
    $dlname1 = $child
    Dlistnumber ##Recursive function, if there is a dlist contained within, it calls itself...
    }
    }
    #Return $Global:count
    }
    ##----------------------------------------------------##

    $smtpclient = new-object Net.Mail.SmtpClient
    $smtp = New-Object system.net.mail.mailmessage
    $smtp.IsBodyHtml = 1
    $smtp.Headers.Add(”message-id”, “<3BD50098E401463AA228377848493927-1>”)
    $smtp.to.add("ITTeam@domain.com")
    #$smtp.to.add("Presence@domain.com") ##smaller batch of people for testing purposes.
    $smtp.from = "DlistCounter@domain.com"
    $smtpclient.host = "SMTPring.domain.com"

    Get-DistributionGroup -resultsize unlimited -filter {acceptmessagesonlyfrom -eq $null -and AcceptmessagesonlyfromDLMembers -eq $null} | Select DisplayName, Identity, Samaccountname, Managedby, RequireSenderAuthenticationEnabled, Grouptype | Export-Csv F:\scripts\DLIST\notlocked\Distro-NotLocked_$time.csv -notypeinfo

    ## since get-distributiongroup, doesn't have a way to count members, and Get-group, doesn't have a way to lock down a list... i had to export the get-distributiongroup, and import them to run against a get-group statement

    $data = Import-CSV F:\scripts\DLIST\notlocked\Distro-NotLocked_$time.csv


    $smtp.body = "Display Name | Members | Managed By | Require Senders Authenticate

    " ##starts building the body of the email
    write-host "Display Name | Members | Managed By" ##log purposes

    ForEach($line in $data)
    {
    $Global:count = 0 ##resets the member count
    $step1 = $line.identity
    If ($step1 -ne $null)
    {
    $Global:dlname = $step1 ##sets the global variable to the information imported.
    $Global:asdf = 0 ##for function purposes
    #write-host "calling the function on:" $line.identity ##left over from troubleshooting
    DListnumber ##calls the function.

    if ($Global:count -gt 250)
    {
    $emailcleanup = get-group $line.identity
    $emailcleanup2 = Get-distributiongroup $line.identity
    $smtp.body += $emailcleanup.displayname + " | " + $Global:count + " | " + $emailcleanup.managedby.name + " | " + $emailcleanup2.requiresenderauthenticationenabled + "

    " ##Adds to the body of the email
    Write-host $line.displayname "|" $Global:Count "|" $line.managedby ##adds to the log file.
    $y = 1 ##set to make sure an email isn't sent if no dlists are over 250
    }
    }
    else
    {
    Write-Host $line.Displayname "| Errors on the account prevented counting members" ##added so that any errors would be noted, usually errors due to special characters in the identity of the distribution group. (i.e. extended hyphens)
    }
    }

    If ($y -eq 1) ##checks to make sure that there were items in the email, then sends the email after adding a few extra notes...
    {
    $smtp.subject = "Distribution Lists, Not Locked down, over 250 users, Check complete"
    $smtp.body += "



    For more information go to \\Server\DList-Logs\" + "Distro_Log_$time.wri NOTE: if you do not have permission to this folder, please navigate to Server, F:\Scripts\DLIST\Logs\ and open the file from there.


    For a Full List of all Distribution Lists that are not currently Locked down, Please go to \\Server\DList-NotLocked\Distro-Notlocked_$time.csv"
    #$smtpclient = new-object Net.Mail.SmtpClient($smtpServer)
    $smtpclient.Send($smtp)
    }



    stop-transcript ##stops the log file and closes it out.
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:457
    Avatar

    --
    21 Nov 2011 08:57 AM
    See "Get-DistributionGroupMember Recursively"
    http://unlockpowershell.wordpress.c...cursively/

    Karl
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    ShabaUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    26 Nov 2011 07:25 AM
    The direct option is by using quest ad commandlets.

    Get-QADGroupMember -Indirect -Identity "notifications" |Export-Csv notifications.csv

    Cheers
    Shaba
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer