Consolidate results into one CSV

Welcome Forums General PowerShell Q&A Consolidate results into one CSV

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #103879

    Participant
    Points: 0
    Rank: Member

    Hello again PS org family!

    Just wanted to get your take on the following and was hoping to obtain some information on how to accomplish my goal.

    Thanks as always in advance,

    Alex

    # Goal = Obtain members of a Distribution Group and the members of each nested groups; then export data into a single csv file
    # Note: Group members will consist of both static and dynamic dgs all nested into the Distribution Group
    # Issue = Exported data results in multiple csv files, but does contain the correct data

    # Sample code that produces separate csv files

    Write-Output 'Starting Script'
    
    # Obtain a Distribution Group data and assign to variable
    
    $Dg = Get-DistributionGroup '(DistributionGroupName)'
    $DgMem = $AllUserWorld | Get-DistributionGroupMember
    
    # Loop through each members 
    
    foreach ($Member in $DgMem){
    
        $Unique = Get-Date -Format yyyymmdd
       
    # Check type via switch and process corresponding switch statement
        
        Switch ($Member.RecipientType) {
        
    # Condition for recipienttype MailUniversalDistributionGroup (static dg)
    
        "MailUniversalDistributionGroup"{"$($Member.Name) is a DG"
    
         Write-Output "Processing $($Member.Name)"
         
         Get-DistributionGroupMember $Member.Name | select @{l='DGName';e={$Member.Name}},name,RecipientType | Export-Csv 
         \\$env:COMPUTERNAME\e$\Temp\$Unique.DG.csv -NoTypeInformation -Append
    
        } # end switch statement for MailUniversalDistributionGroup condition
    
    # Condition for recipienttype DynamicDistributionGroup (dynamic)
       
        "DynamicDistributionGroup"{"$($Member.Name) is a DDG"
        
        Write-Output "Processing $($Member.Name)"
    
        $DDG = Get-DynamicDistributionGroup $Member.Name
        Get-Recipient -RecipientPreviewFilter $DDG.RecipientFilter -ResultSize unlimited | select @{l='DDGName';e= 
        {$($DDG.Name)}},name,RecipientType | Export-Csv \\$env:COMPUTERNAME\e$\Temp\$Unique.DDG.csv -NoTypeInformation -Append
        
        } # end switch statement for DynamicDistributionGroup
    
       } # end switch
    
    } # end for each $Member
    
  • #104042

    Keymaster
    Points: 1,524
    Helping HandTeam Member
    Rank: Community Hero

    It's a little hard to guess what this is doing by just reading the code. You say you're getting.. are you getting the right data in multiple CSV files, and just want them joined? Maybe I'm not understanding the goal vs. what's actually happening.

    • #104057

      Participant
      Points: 0
      Rank: Member

      My apologies if my code presentation is not clear. ( Still trying to learn and practice proper formatting).

      My expected results as far as the data are correct however it's how it is being exported. Instead of one csv file I am getting multiple, in actuality two for the DG and a few for the DDG. My hopes were to consolidate the data into one csv file for the DG and one for the DDG.

      The data in those csv files are that of what I was after or intended on getting.

      Thanks for you time.

  • #104062

    Keymaster
    Points: 1,524
    Helping HandTeam Member
    Rank: Community Hero

    Yyyyyyyeah... so, I obviously can't run your code in your environment, so it's tough to diagnose. The problem is in how you're running your queries, though, so I can't help. I can tell you what I'd do, though.

    I'd debug it.

    I'd add a Write-Debug statement after I populated each variable. I'd run the script with -Debug, and I'd (S)uspend the script each time it hit Write-Debug. Then I'd check the variable I'd just populated to see what it contained. At some point, you've got a variable with something other than what you expected, and that's how you find it. Run Continue to resume running the script to the next breakpoint.

    • #104357

      Participant
      Points: 0
      Rank: Member

      Hi Don,

      Just wondering if you could please show me an example as to how I can debug or implement a write-debug on my script. I researched online but I am still not following.

      Thanks,

      Alex

  • #108523

    Participant
    Points: 0
    Rank: Member

    Researched online and I am now able to incorporate Write-Debug during my script creation/testing.

    Thank again!

You must be logged in to reply to this topic.