Link to home
Start Free TrialLog in
Avatar of WWUSteve
WWUSteve

asked on

Powershell Exchange Distribution list

Hello, I am having issues with a powershell script I'm trying to run. I only posted a sub section with the part that is having an issue.

$NewAllCurrentStudents is an imported .CSV that has headers such as CUR_STUD_DIV, PT_FT_STS, and LOC_CDE

The issue I'm having is the CSV has users that mach the 2 online groups but it keeps putting them in the 2 Evening students group. Does anyone have a clue as to why this may we happening?

Thanks!

foreach ($user in $NewAllCurrentStudents)
        {
            $error.clear()
            if (($user.EMAIL_ADDRESS -like "*@test.domain.com") -or ($user.EMAIL_ADDRESS -like "*@domain.com"))
            {
               
                if ($user.CUR_STUD_DIV -eq "SU" -and $user.PT_FT_STS -eq "F"){Add-DistributionGroupMember -Identity "Traditional Full-Time Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "SU" -and $user.PT_FT_STS -eq "P"){Add-DistributionGroupMember -Identity "Traditional Part-Time Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AG" -and $user.LOC_CDE -ne "ONL"){Add-DistributionGroupMember -Identity "Off Campus Evening Graduate Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AU" -and $user.LOC_CDE -ne "ONL"){Add-DistributionGroupMember -Identity "Off Campus Evening Undergraduate Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AG" -and $user.LOC_CDE -eq "ONL"){Add-DistributionGroupMember -Identity "Off Campus Online Graduate Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AU" -and $user.LOC_CDE -eq "ONL"){Add-DistributionGroupMember -Identity "Off Campus Online Undergraduate Students" -Member $user.EMAIL_ADDRESS}
               
                else {$incorrectlyProvisionedStudents = $incorrectlyProvisionedStudents + $user}

Open in new window

Avatar of M A
M A
Flag of United States of America image

If you explain what are you trying to achieve we would be able to propose better solution.
Please explain if you don't mind
Avatar of WWUSteve
WWUSteve

ASKER

No problem, this script is run once every 24 hours to delete and readd my students into distribution lists so that my staff has a very accurate list to notify them of events. So the list and the users get deleted and recreated once a day.
I guess I also don't fully understand how powershell crawls a .CSV either. I assumed it goes line by line, without ever going back thru the list after it's finished with that line.
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When powershell goes through a CSV it goes Row by Row using the column headings as indicators. Based on what you have above you do not have a closing brace "}" for your ForEach statement or your initial If Statment. I know that you said you only posted part of the code however if these are not in place then it will fail. Aside from that everything else look fine. It should look like below...

foreach ($user in $NewAllCurrentStudents)
        {
            $error.clear()
            if (($user.EMAIL_ADDRESS -like "*@test.domain.com") -or ($user.EMAIL_ADDRESS -like "*@domain.com"))
            {
               
                if ($user.CUR_STUD_DIV -eq "SU" -and $user.PT_FT_STS -eq "F"){Add-DistributionGroupMember -Identity "Traditional Full-Time Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "SU" -and $user.PT_FT_STS -eq "P"){Add-DistributionGroupMember -Identity "Traditional Part-Time Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AG" -and $user.LOC_CDE -ne "ONL"){Add-DistributionGroupMember -Identity "Off Campus Evening Graduate Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AU" -and $user.LOC_CDE -ne "ONL"){Add-DistributionGroupMember -Identity "Off Campus Evening Undergraduate Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AG" -and $user.LOC_CDE -eq "ONL"){Add-DistributionGroupMember -Identity "Off Campus Online Graduate Students" -Member $user.EMAIL_ADDRESS}
                if ($user.CUR_STUD_DIV -eq "AU" -and $user.LOC_CDE -eq "ONL"){Add-DistributionGroupMember -Identity "Off Campus Online Undergraduate Students" -Member $user.EMAIL_ADDRESS}
               
                else {$incorrectlyProvisionedStudents = $incorrectlyProvisionedStudents + $user}
            }
}

Open in new window


Will.
I would be happy to post the entire script if it would help, I just need to sanitize the data first.
Thank you footech! You nailed it! There were trailing spaces in my .csv that I never noticed!

Now it works perfectly!