Hi
Im trying to move user accounts based on a set of filters on a csv file to the appropriate OU on that same file. I want to grab all users in AD that match the teachers name and grade level for each row, and move them to the OU listed for that same row.
Here is how the date is configured on the CSV file
School | Grade | Teacher | ADOU |
QCV | 0 | Jane Doe | OU=Teacher1,OU=Grade 0,DC=students,DC=local |
Here's the script Im using:
import-module activedirectory
$mappings = Import-Csv -Path "C:\studentsAccounts\Student_AD_ImportSorted.csv"
foreach ($map in $mappings){
$grade=$map.'grade'
$teacher=$map.'Teacher'
$OU=$map.'ADOU'
Get-ADUser -server "ho-sb-dc03" -Filter {(Department -eq "$grade") -and (Title -eq "$teacher")} | Move-ADObject -Server "ho-sb-dc03" -TargetPath $OU
}
When i run it, i get an error about the $OU variable being null.
Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\studentsAccounts\MoveNewStudents.ps1:9 char:149
+ Get-ADUser -server "ho-sb-dc03" -Filter {(Department -eq "$grade") -and (Title -eq "$teacher")} | Move-ADObject -Server "ho-sb-dc03" -TargetPath <<<< $OU
+ CategoryInfo : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject