Ok Rob! I tried your suggestion could you take a look at it? I am still receiving 99 moved.
$FileName = (Get-Date).tostring("MM-dd-yyyy-hh-mmt")
$FileName = "c:\powershell\StudentImport$($FileName).log"
$T = Get-Date -Uformat "%m-%d-%Y %r"
"Started Powershell Student Import $($T)" >> $FileName
[Environment]::NewLine >> $FileName
$Students = Import-Csv C:\student\students.csv
$Results = Foreach ($Student in $Students) {
$Textinfo = (Get-Culture).Textinfo
$FName = $Student.Firstname
$LName = $Student.Lastname
$Midname = $Student.Middlename
$FName = $Textinfo.ToTitleCase($FName.ToLower())
$LName = $Textinfo.ToTitleCase($LName.ToLower())
$Midname = $Textinfo.ToTitleCase($Midname.ToLower())
$Name = "$LName $FName $MidName"
$Sam = $Student.StudentUserID
$CC = $Student.School
$Grade = $Student.Grade
$Pw = $Student.Pin
$NameChange = $Student.NameChange
$Dom = "student.testlab.local"
Switch ( $CC ) {
'0085' {$OU = "OU=AllGrades,OU=Students,OU=BHS,OU=High School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0085 All Students"
}
'0051' {$OU = "OU=AllGrades,OU=Students,OU=SHS,OU=High School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0051 All Students"
}
'0171' {$OU = "OU=AllGrades,OU=Students,OU=Phillippi,OU=Elementary School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0171 All Students"
}
'0031' {$OU = "OU=AllGrades,OU=Students,OU=SMS,OU=Middle School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0031 All Students"
}
'0021' {$OU = "OU=AllGrades,OU=Students,OU=Pineview,OU=Other,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0021 All Students"
}
'0084' {$OU = "OU=AllGrades,OU=Students,OU=BMS,OU=Middle School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0021 All Students"
}
'0131' {$OU = "OU=AllGrades,OU=Students,OU=Fruitville,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0131 All Students"
}
'1291' {$OU = "OU=AllGrades,OU=Students,OU=Woodland,OU=Middle School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1291 All Students"
}
'0101' {$OU = "OU=AllGrades,OU=Students,OU=Brentwood,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0101 All Students"
}
'1282' {$OU= "OU=AllGrades,OU=Students,OU=Tatum,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1282 All Students"
}
'1211' {$OU = "OU=AllGrades,OU=Students,OU=LNS,OU=Other,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1211 All Students"
}
'1231' {$OU = "OU=AllGrades,OU=Students,OU=Toledo,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1231 All Students"
}
'0294' {$OU = "OU=AllGrades,OU=Students,OU=Triad-Second Chance,OU=Other,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0294 All Students"
}
'0141' {$OU = "OU=AllGrades,OU=Students,OU=Mcintosh,OU=Middle School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0141 All Students"
}
'1261' {$OU = "OU=AllGrades,OU=Students,OU=Heron,OU=Middle School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1261 All Students"
}
'0221' {$OU = "OU=AllGrades,OU=Students,OU=VHS,OU=High School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0221 All Students"
}
'0271' {$OU = "OU=AllGrades,OU=Students,OU=GulfGate,OU=Elementary School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0271 All Students"
}
'0291' {$OU = "OU=AllGrades,OU=Students,OU=Wilkinson,OU=Elementary School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0291 All Students"
}
'0012' {$OU = "OU=AllGrades,OU=Students,OU=AltaVista,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0012 All Students"
}
'0071' {$OU = "OU=AllGrades,OU=Students,OU=BayHaven,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0071 All Students"
}
'0501' {$OU = "OU=AllGrades,OU=Students,OU=Emma,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0501 All Students"
}
'0261' {$OU = "OU=AllGrades,OU=Students,OU=Gocio,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0261 All Students"
}
'1241' {$OU = "OU=AllGrades,OU=Students,OU=Atwater,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1241 All Students"
}
'0121' {$OU = "OU=AllGrades,OU=Students,OU=Englewood,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0121 All Students"
}
'0381' {$OU = "OU=AllGrades,OU=Students,OU=Garden,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0381 All Students"
}
'0461' {$OU = "OU=AllGrades,OU=Students,OU=Glenallen,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0461 All Students"
}
'1341' {$OU = "OU=AllGrades,OU=Students,OU=Lamarque,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1341 All Students"
}
'0201' {$OU = "OU=AllGrades,OU=Students,OU=Tuttle,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0201 All Students"
}
'0301' {$OU = "OU=AllGrades,OU=Students,OU=Ashton,OU=Elementary School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0301 All Students"
}
'1251' {$OU = "OU=AllGrades,OU=Students,OU=NPHS,OU=High School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1251 All Students"
}
'0191' {$OU = "OU=Allgrades,OU=Students,OU=Southside,OU=Elementary School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0191 All Students"
}
'0181' {$OU = "OU=AllGrades,OU=Students,OU=Riverview HS,OU=High School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0181 All Students"
}
'0111' {$OU = "OU=AllGrades,OU=Students,OU=Brookside,OU=Middle School,OU=North,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0111 All Students"
}
'0293' {$OU = "OU=AllGrades,OU=Students,OU=OakPark,OU=Other,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0293 All Students"
}
'0471' {$OU = "OU=AllGrades,OU=Students,OU=Lakeview,OU=Elementary School,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0471 All Students"
}
'0211' {$OU = "OU=AllGrades,OU=Students,OU=VES,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0211 All Students"
}
'0451' {$OU = "OU=AllGrades,OU=Students,OU=VMS,OU=Middle School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0451 All Students"
}
'0491' {$OU = "OU=AllGrades,OU=Students,OU=Taylor,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "0491 All Students"
}
'1271' {$OU = "OU=AllGrades,OU=Students,OU=Cranberry,OU=Elementary School,OU=South,OU=Zones,DC=student,DC=testlab,DC=local"
$Group = "1271 All Students"
}
'0292' {$OU = "OU=AllGrades,OU=Students,OU=PSS-ESE,OU=Other,OU=Central,OU=Zones,DC=student,DC=testlab,DC=local"
}
}
$User = Get-ADUser $Sam -Server $Dom
If ( !$User ) {
try {
$NewUserParams = @{
'UserPrincipalName' = "$Sam@$Dom"
'SamAccountName' = "$Sam"
'Name' = $Name
'GivenName' = "$FName"
'SurName' = "$LName"
'DisplayName' = $LName + " " + $FName
'Description' = "$($sam) $($CC) $($Grade)"
'Homedirectory' = "\\SCS-STUDATA\studenthfolders\$Sam"
'Homedrive' = "H:"
'Title' = "Student"
'Department' = $Grade
'Company' = $CC
'Path' = "$OU"
'AccountPassword' = (ConvertTo-SecureString $Pw -AsPlainText -Force)
'PasswordNeverExpires' = $true
'CannotChangePassword' = $true
'Enabled' = $true
'Server' = "dc2.student.testlab.local"
}
New-ADUser @NewUserParams
$student | Select-Object -Property *, @{Name='Status';Expression={'CREATE'}}
}
catch {
$msg = 'This Failed custom message. {0}' -f $_
$Student | Select-Object -Property *, @{Name='Status';Expression={'CREATE_FAIL:{0}' -f $msg}}}
}
else {
try {
If ($User.Company -ne $Student.School) {
$User | Set-ADuser -Company $Student.School -Description "$($sam) $($CC) $($Grade)" -Department $Student.Grade -Server $Dom
Move-ADObject -Identity $User.Distinguishedname -server $Dom -TargetPath $OU -ErrorAction Stop
$Student | Select-Object -Property *, @{Name='Status';Expression={'MOVE'}}
}
}
catch {
$msg = 'This failed custom message. {0}' -f $_
$Student | Select-Object -Property *, @{Name='Status';Expression={'MOVE_FAIL:{0}'}}
}
}
}
#$CreateCount = ($Results | Where{$_.Status -eq 'CREATE'}).count
#$MoveFailCount = ($Results | Where{$_.Status -eq 'MOVE_FAIL:*'}).count
$Results | Group-Object -Property Status -NoElement
This is the output:
PS C:\Powershell> .\Student_Testing_2.ps1
Count Name
----- ----
99 MOVE
PS C:\Powershell>