I’m new to scripting and am attempting to create a sript that will process data from a csv file into variables that I can use to automatically create users in Active Directory. The file contains a grade level that must be converted to graduation year to be used in proper nameing of the user account and the proper placement of the the user account into the OUs in Active Directory.
Currently, the code written will read the csv file 4 times (12 times if completed this way). This seems like a lot of overhead. Being able to reference the grade level to graduating class in the lines would really be helpful but the order of syntax is eluding me.
Graduation years will remain the same for users but the grade changes every year. I don’t want to have to change 24+ lines of code every school year (that’s just asking for a problem). I thought I could reference a hash table inline to make appropriate changes and also limit the number of reads from the csv file.
######################################
## Create, Disable Student Accounts ##
## from file imported from DASL ##
######################################
# Change Current School Year
$currentYear = 2014
##Build grade_code to ClassOf table
$Classof =@{
"12" = $currentYear++
"11" = $currentYear++
"10" = $currentYear++
"9" = $currentYear++
"8" = $currentYear++
"7" = $currentYear++
"6" = $currentYear++
"5" = $currentYear++
"4" = $currentYear++
"3" = $currentYear++
}
##Import Student Data, Removing apostrophies and hypens from certain fields.
##Description changed based on graduation Year
$AllStudents = import-csv C:\StuGroups\Students.csv
$2014Students = import-csv C:\StuGroups\Students.csv | Where-Object {$_.grade_code -eq "12"} |
Select student_code,last_name,first_name,grade_code,
@{n='DisplayName';e={$_.last_name + ", " + $_.first_name}},
@{n='loginName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='email';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","") + "@school.org"}},
@{n='description';e={"Class of 2014 Student ID :" + $_.student_code}},
@{n='sAMAccountName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='Title';e={"Student"}}
$2015Students = import-csv C:\StuGroups\Students.csv |Where-Object {$_.grade_code -eq "11"} |
Select student_code,last_name,first_name,grade_code,
@{n='DisplayName';e={$_.last_name + ", " + $_.first_name}},
@{n='loginName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='email';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","") + "@school.org"}},
@{n='description';e={"Class of 2015 Student ID :" + $_.student_code}},
@{n='sAMAccountName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='Title';e={"Student"}}
$2016Students = import-csv C:\StuGroups\Students.csv |Where-Object {$_.grade_code -eq "10"} |
Select student_code,last_name,first_name,grade_code,
@{n='DisplayName';e={$_.last_name + ", " + $_.first_name}},
@{n='loginName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='email';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","") + "@school.org"}},
@{n='description';e={"Class of 2016 Student ID :" + $_.student_code}},
@{n='sAMAccountName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='Title';e={"Student"}}
$2017Students = import-csv C:\StuGroups\Students.csv |Where-Object {$_.grade_code -eq "9"} |
Select student_code,last_name,first_name,grade_code,
@{n='DisplayName';e={$_.last_name + ", " + $_.first_name}},
@{n='loginName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='email';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","") + "@school.org"}},
@{n='description';e={"Class of 2017 Student ID :" + $_.student_code}},
@{n='sAMAccountName';e={$_.last_name.replace("-","").replace("backtick'","") + "." + $_.first_name.replace("-","").replace("backtick'","")}},
@{n='Title';e={"Student"}}
## AND 8 MORE GRADES AFTER THE LAST ONE ABOVE
$2014Students | Export-Csv C:\StuGroups\2014Students.csv -NoTypeInformation
$2015Students | Export-Csv C:\StuGroups\2015Students.csv -NoTypeInformation
$Classof
#Write-Output $2016Students.count
#Write-Output $2017Students.count