none
Import Export All distributiongroupmembers

    Domanda

  • Below is about Exchange, but I need an Powershell script solution.
    I hope I posted in the correct forum.

    Situation:
    We have 2 forests with Exchange and all content needs to be moved (both Exchange 2010).

    What I need to accomplish right now:

    Transfer all distributiongroups including it's members to the new forest.
    This is what I got what is working(all users & mailcontacts are copied, not to worry about it's members).


    #Export all distributiongroups to Csv
    Get-DistributionGroup | Select-Object Name,Alias,DisplayName,PrimarySmtpAddress | Export-Csv c:\temp\ExportDistroGroups.csv

    #Import all distributiongroups from Csv
    Import-Csv c:\temp\ExportDistroGroups.csv | ForEach-Object{New-Distributiongroup -Name $_.Name -DisplayName $_.DisplayName -PrimarySmtpAddress $_.PrimarySmtpAddress -OrganizationalUnit "Domain/OU/GROUPs"}

    # Get all distributiongroup members and export it to an Csv

    $dist = get-distributiongroup
    $dist |
    Select-object Name,
    @{n="members";e={foreach($i in(get-distributiongroupmember $_)){$i.primarysmtpaddress}}}|
    Export-csv -Path c:\temp\test.csv -notype

    The Csv looks like this:

    Name,Members
    "GroupName1",bla@bla.com bla2@bla.com Bla3@bla.com
    "GroupName2",asd@bla.com asd2@bla.com asd3@bla.com

    !
    Ok now, the 'Challenge'.
    I have this Csv file and need to create some script so that I can add the users to the distributiongroups.
    The basic cmdlet with parameters are as following:
    Add-distributiongroupmember -Identity "Groupname" -member "Member"

    Unfortunately this cmdlet won't allow to add more than one member.
    The the solution would be simpel with something like:
    import-csv c:\temp\test.csv | foreach $i in ($_){Add-distributiongroupmember -Identity $i.Name -member $i.member}
    Output would be:
    Add-distributiongroupmember -identity GroupName1 -member  bla@bla.com bla2@bla.com Bla3@bla.com

    But it doesn't work.

    Pff, sorry for the long story.

    So I need something that will go through each record in the Csv file and create the command for each 'Word' in member (2nd Array).

    Hope this makes senses, if not please let me know if I can clearify anything.



    domenica 3 giugno 2012 21:49

Risposte

  • I think this should work:

    import-csv c:\temp\test.csv | foreach-object{
      foreach( $m in @( -split $_.members) )
      {
        Add-distributiongroupmember -Identity $_.Name -member $m
      }
    }

    Can you test once?

    Reinhard

    • Contrassegnato come risposta Thom Kortekaas lunedì 4 giugno 2012 10:33
    lunedì 4 giugno 2012 06:38

Tutte le risposte

  • I think this should work:

    import-csv c:\temp\test.csv | foreach-object{
      foreach( $m in @( -split $_.members) )
      {
        Add-distributiongroupmember -Identity $_.Name -member $m
      }
    }

    Can you test once?

    Reinhard

    • Contrassegnato come risposta Thom Kortekaas lunedì 4 giugno 2012 10:33
    lunedì 4 giugno 2012 06:38
  • Thank you Reinhard, works like a charm !!
    • Proposto come risposta dheenaPM martedì 26 giugno 2012 19:24
    • Proposta come risposta annullata dheenaPM martedì 26 giugno 2012 19:25
    lunedì 4 giugno 2012 11:17
  • # Retrieve all Distribution Groups
    $temp = Get-DistributionGroup -Anr Conto
    foreach($group in $temp)
    {
    Get-DistributionGroup -Anr $group | Format-Table Name, ManagedBy
    Get-DistributionGroup -identity $group | Get-ADPermission | where {($_.ExtendedRights -like “*Send-As*”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”)} | FT -Wrap
    write-output "GroupName:$group "
    Write-output "GroupMembers:"
    Get-DistributionGroupMember $group |ft displayname,alias,primarysmtpaddress
    write-output ‘ ‘
    }

    You can use this script to fetch all the details of all DG's

    martedì 26 giugno 2012 19:26