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

WWUSteveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MAS (MVE)EE Solution GuideCommented:
If you explain what are you trying to achieve we would be able to propose better solution.
Please explain if you don't mind
0
WWUSteveAuthor Commented:
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.
0
WWUSteveAuthor Commented:
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.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

footechCommented:
That's correct, each line gets read in and a matching object is created.  When looping through the contents (like with foreach), each loop only handles a single object and then moves on, PS won't go back through again (unless you did another loop).

I don't see anything wrong with the code.  You might check your .CSV to see if there are perhaps leading or trailing spaces, particularly in the LOC_CDE fields.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
WWUSteveAuthor Commented:
I would be happy to post the entire script if it would help, I just need to sanitize the data first.
0
WWUSteveAuthor Commented:
Thank you footech! You nailed it! There were trailing spaces in my .csv that I never noticed!

Now it works perfectly!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.