Home > Net >  Import CSV with loop problem - The result contains only last item from the CSV
Import CSV with loop problem - The result contains only last item from the CSV

Time:01-30

I need to import a few groups from a CSV file and then export members - but I need it like one row each, something like:

"Group_name1", "member1, member2,member3"

"Group_name2", "member1,member2,member3"

"Group_name3", "member1,member2,member3"

And my script is working fine for a single group but I'm having troubles with adding a for-each loop - the result contains only last item from the CSV..

              #$DL='plum'
       $DL_List = "C:\ps1\shared_mailboxes\groups.csv"
       $DL_array = (Import-Csv -Path $DL_List).name
       foreach ($DL in $DL_array)
       {
        $DL_Membership = (Get-DistributionGroupMember -identity $DL).displayName
    if([string]$DL_Membership -ne "")
    {
     $Members = ""
     foreach($DL_Membership in $DL_Membership)
     {
      if($Members -ne "")
      {
       $Members=$Members  ","
      }
      $Members = $Members   $DL_Membership
     }
     
    }
       }

    $ExportCSV=".\group_members_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
    $Result = @{'Group'=$DL;'Users'=$Members}
    $Results = New-Object PSObject -Property $Result
    $Results |select-object 'Group','Users' | Export-Csv -Path $ExportCSV -Notype -Append
   
   

I googled it but I'm not sure what I should change in my script..

CodePudding user response:

The script has been re-writed using -join operator and now it's working as excepted.

 $DL_List = Import-Csv "C:\ps1\shared_mailboxes\groups.csv"

$CsvOut = foreach ( $dl in $DL_List ) {
  $Group       = $dl.name
  $MemberArray = (Get-DistributionGroupMember -identity $Group).displayName
  $MemberList  = $MemberArray -join ','
  # generate the output object
  # that goes in $CsvOut
  [PSCustomObject] @{
    Group   = $Group
    Members = $MemberList
  }  
}

$Timestamp  = Get-Date -Format 'yyyy-MM-dd-HHmm' # this is a string
$CsvOutPath = "c:\ps1\shared_mailboxes\memberlist.$Timestamp.csv"

$CsvOut | Export-Csv $CsvOutPath -NoTypeInfo  

CodePudding user response:

I think you are over-complicating this. If your desired result is a CSV file, then output objects straight away like below:

$DL_List  = 'C:\ps1\shared_mailboxes\groups.csv'
$DL_array = (Import-Csv -Path $DL_List).name
$outFile  = '.\group_members_{0:yyyy-MMM-dd-ddd hh-mm}.csv' -f (Get-Date)

$result   = foreach ($DL in $DL_array) {
    # output an object with the name of the DL and its members
    [PsCustomObject]@{
        Group = $DL
        Users = (($DL | Get-DistributionGroupMember).displayName | 
                        Where-Object {$_ -match '\S'}) -join ','
    }
}

$result | Export-Csv -Path $outFile -NoTypeInformation

Using calculated properties, you can also do this without a foreach loop:

$DL_List  = 'C:\ps1\shared_mailboxes\groups.csv'
$DL_array = (Import-Csv -Path $DL_List).name
$outFile  = '.\group_members_{0:yyyy-MMM-dd-ddd hh-mm}.csv' -f (Get-Date)

$DL_array | Select-Object @{Name = 'Group'; Expresssion = {$_}},
                          @{Name = 'Users'; Expresssion = {
                              (($_ | Get-DistributionGroupMember).displayName | 
                                     Where-Object {$_ -match '\S'}) -join ','
                           }} |
            Export-Csv -Path $outFile -NoTypeInformation 
  • Related