Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Help Simplifying Code

$
0
0

Argh. It was all working fine with the changes. I was working on another script and when I came back to this one I receive this:
Index was outside the bounds of the array.
At C:\StuGroups\StudentAccounts.ps1:49 char:5
+ New-Object PSObject -Property @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException

Current Script with backticks removed.

######################################
## Create, Disable Student Accounts ##
##   from file imported from DASL   ##
######################################
 
# Change Current School Year
$currentYear = 2014
##Build grade_code to ClassOf table
$Classof =@{
    "23″ = "23″ #special education code
    "12″ = $currentYear++
    "11″ = $currentYear++
    "10″ = $currentYear++
    "9″ = $currentYear
    "09″ = $currentYear++
    "8″ = $currentYear
    "08″ = $currentYear++
    "7″ = $currentYear
    "07″ = $currentYear++
    "6″ = $currentYear
    "06″ = $currentYear++
    "5″ = $currentYear
    "05″ = $currentYear++
    "4″ = $currentYear
    "04″ = $currentYear++
    "3″ = $currentYear
    "03″ = $currentYear++
 
    }
##Build school_code to AD OU table
$Bldng = @{
    "TAHS" = "THS"
    "TAMS" = "TMS"
    "TAMU" = "MUN"
    }
$BldngName = @{
    "TAHS" = "City High School"
    "TAMS" = "City Middle School"
    "TAMU" = "City Elementary School"
    }
$AllStudentsCsv = Import-Csv C:\StuGroups\Students.csv | Where-Object {$_.school_code -ne "TADU"}
$AllStudents = foreach($student in $AllStudentsCsv) {
    # Let's not waste CPU cycles and screen space doing the same thing over and over
    $firstNameFiltered = $student.first_name.Replace("-","").Replace(".","").Replace("-","")
    $lastNameFiltered = $student.last_name.Replace("-","").Replace("-","")
    $password = ($student.student_code + $firstNameFiltered + $firstNameFiltered).Substring(0,8)
    $loginName = ($lastNameFiltered + "." + $firstNameFiltered).Substring(0.20)
    # More string formatting, less piecing bits of string together
    New-Object PSObject -Property @{
        'first_name' = "{0}" -f $student.first_name
        'last_name' = "{0}" -f $student.last_name
        'StudentID' = "{0}" -f $student.student_code
        'DisplayName' = "{0}, {1}" -f $student.last_name, $student.first_name
        'loginName' = ($lastNameFiltered + "." + $firstNameFiltered)[0..19] -join "
        'email' = "{0}.{1}@stu.cityschools.org" -f $lastNameFiltered, $firstNameFiltered
        'description' = "Class of {0} Student ID: {1}" -f $Classof[$student.grade_code], $student.student_code
        'grade' = "{0}" -f $student.grade_code
        'Title' = 'Student'
        'Password' = "{0}" -f $password
        'Bldng' = "{0}" -f $Bldng[$student.school_code]
        'Path' = "OU={0},ou=students,OU=Users,OU={1},OU=Test" -f $Classof[$student.grade_code],$Bldng[$student.school_code]
        'userPrincipalName' = "{0}.{1}@CitySchools.local" -f $lastNameFiltered, $firstNameFiltered
        'Department' = "{0}" -f $BldngName[$student.school_code]
        'Office' = "{0}" -f $Classof[$student.grade_code]
        'Implement' = "yes"
    }
}
$Allstudents | Export-Csv C:\StuGroups\AllStudents.csv -NoTypeInformation
## Call create_ad_users.ps1 to create accounts
Attachments:
You must be logged in to view attached files.

Viewing all articles
Browse latest Browse all 13067

Trending Articles