Exchange 2010 Distribution Groups

Posted by robd on January 12, 2012
Batch Scripts
Recently I needed to re-create some distribution groups in a new Exchange 2010 environment from an Exchange 2003 environment
 
Here’s how I did it:
 
Firstly extracted the groups from Active Directory by modifying a script taken the good people over at Migration Wiz (Their Script http://migrationwiz.zendesk.com/entries/20046721-export-active-directory-distribution-lists-to-csv)

 

My Script (basically less information in the csv file) :


'********************************************************************************
' Description: Exports all distribution lists to CSV
'********************************************************************************

dim filename, props
filename = "2003DistributionLists.csv"
props = array("displayName","sAMAccountName",”Mail”,”mailNickname”)

'********************************************************************************
' Helper Functions
'********************************************************************************

function RetrieveUsers()

    dim rootDse, defaultNamingContext, ldapQuery, ldap, conn, rs

    set rootDse = GetObject("LDAP://RootDSE")
    defaultNamingContext = rootDse.Get("defaultNamingContext")
    ldapQuery = "(&(objectClass=group)(mailNickname=*)(mail=*))"
    ldap = "<LDAP://" & defaultNamingContext & ">;" & ldapQuery & ";adspath;subtree"

    set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
    set RetrieveUsers = conn.Execute(ldap)

end function

function ArrayToString(value)

    dim firstValue, result
    firstValue = true
    result = ""

    for i=0 to UBound(value)
        if not firstValue then
            result = result + ";"
        end if
        result = result + value(i)
        firstValue = false
    next

    ArrayToString = result

end function

function DnToEmailAddress(dn)

    dim ldap, mail

    mail = ""
    ldap = "LDAP://" & dn
    set user = GetObject(ldap)
    on error resume next
    mail = user.Get("mail")
    on error goto 0

    DnToEmailAddress = mail

end function
function Main()

    dim value, fso, shell, users, firstProp, dns, firstDn, memberEmail, emailAddress

    set fso = CreateObject("Scripting.FileSystemObject")
    set csv = fso.CreateTextFile(filename)
    if not err.number = vbEmpty then
        msgbox err.message, 0, "ExportError"
        exit function
    end If

    comma = false
    for each prop in props
        if comma then
            csv.Write(",")
        end if
        csv.Write("""")
        csv.Write(prop)
        csv.Write("""")
        comma = true
    next
    csv.WriteLine(",""memberEmail""")

    set users = RetrieveUsers()
    while not users.EOF
        set user = GetObject(users.Fields(0).Value)

        memberEmail = ""
        firstProp = true
        for each prop in props
            if not firstProp then
                csv.Write(",")
            end if
            csv.Write("""")
            value = ""
            on error resume next
            value = user.Get(prop)
            if IsArray(value) then
                value = ArrayToString(value)
                if "member" = prop then
                    dns = Split(value, ";")
                    firstDn = true
                    for i=0 to UBound(dns)
                        emailAddress = DnToEmailAddress(dns(i))
                        if not "" = emailAddress then
                            if not firstDn then
                                memberEmail = memberEmail + ";"
                            end if
                            memberEmail = memberEmail + emailAddress
                            firstDn = false
                        end if
                    next
                end if
            end if
            csv.Write(value)
            on error goto 0
            csv.Write("""")
            firstProp = false
        next
        csv.WriteLine(",""" & memberEmail & """")

        users.MoveNext
    wend

    csv.Close

    set csv = nothing
    set fso = nothing

    Set shell = CreateObject("WScript.Shell")
    shell.run ("Explorer" & " ." )
end function

Main


You should now have a csv file containing all your information!!

 

Next create a PowerShell command to import the csv file, here’s one I made earlier:
Import-Csv user2.csv |
foreach {New-DistributionGroup -Alias $_.mailnickname -DisplayName $_.displayName
-Type Distribution -Name $_.displayName -PrimarySmtpAddress $_.mail
-OrganizationalUnit "OU=Users,OU=Sites,DC=DOMAIN,DC=local" }

 

Basically the headers from the csv file need to match the sections in the powershell command that start with $_.

 

Once that runs in powershell without issue then you should be good to import users into the groups….which I also have a script for!!

1 Comment to Exchange 2010 Distribution Groups

  • If you have less than 100 grams carbs day, some protein is used for making glucose in the liver. However with these guidelines you should still get enough. Also I’ve found a number of people feel better with some starchy carbs at most meals, about a fist size. And it frequently improves fat loss when they control their portion of protein / fat.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.