0

This script will create the new users, but it won't then move them based on the user's description? Edit, it appears that Powershell is showing the description field as blank, but it's populated in Active Directory.

[CmdletBinding()]
        param(
        [Parameter(Mandatory=$true)][string]$filepath #Require file path to import new users
        )

        Import-Module ActiveDirectory #import AD module

        Import-Csv -Path $filepath | New-ADUser -PassThru -OutVariable newusernames #send the new user CSV to new-ADUser and create a variable with the new user accounts.

        foreach($newuser in $newusernames)
        {

        switch -Wildcard ($newuser.Description)
                    {
                      "*Kindergartner*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=KindergartenStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*1stGrader*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=1stGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*2ndGrader*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=2ndGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*3rdGrader*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=3rdGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*4thGrader*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=4thGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*5thGrader*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=5thGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*6thGrader*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=6thGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
                      "*Teacher*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=TeachersOU,OU=GrandRidgeStaff,DC=dgwphotos,DC=local" }
                      "*Administration*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=AdministrationOU,OU=GrandRidgeStaff,DC=dgwphotos,DC=local" }
                      "*ClassifiedStaff*" {Move-ADObject -Identity $newuser.DistinguishedName -TargetPath "OU=ClassifiedStaffOU,OU=GrandRidgeStaff,DC=dgwphotos,DC=local" }
                    }

        }
Davidw
  • 1,222
  • 3
  • 14
  • 25
  • Is there any error message? "Failed" is not exactly helpful. Create a default catch-all condition clause so you know if the matches works as you expect. – strongline Sep 16 '17 at 15:56
  • That's the strange thing, it doesn't throw an error, it never seems to get to the foreach loop, though. – Davidw Sep 16 '17 at 16:49
  • Interestingly, it sets the description in AD, but Powershell shows that field as blank. – Davidw Sep 19 '17 at 19:28
  • pipe the returned object to get-member cmdlet, description could be not in the properties at all. – strongline Sep 21 '17 at 02:49

2 Answers2

2

The way you currently have your code structured means it must create all of the users from the CSV file before moving any of them to their final OU. Does the script actually finish? Or are you stopping it prematurely when you don't see the users going into their designated OU?

If you want to have the users moved as they are created, you should either include the Path parameter in your CSV output so they're created in the target OU to begin with or restructure your code so that the move happens immediately following each New-ADUser call.

You could also just tweak your CSV output prior to calling New-ADUser like this:

$tweakedCSV = Import-Csv -Path $filepath | Select *,@{L='Path';E={
    switch -Wildcard $_.description {
        "*Kindergartner*" { "OU=KindergartenStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*1stGrader*" { "OU=1stGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*2ndGrader*" { "OU=2ndGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*3rdGrader*" { "OU=3rdGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*4thGrader*" { "OU=4thGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*5thGrader*" { "OU=5thGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*6thGrader*" { "OU=6thGradeStudentsOU,OU=GrandRidgeStudents,DC=dgwphotos,DC=local" }
        "*Teacher*" { "OU=TeachersOU,OU=GrandRidgeStaff,DC=dgwphotos,DC=local" }
        "*Administration*" { "OU=AdministrationOU,OU=GrandRidgeStaff,DC=dgwphotos,DC=local" }
        "*ClassifiedStaff*" { "OU=ClassifiedStaffOU,OU=GrandRidgeStaff,DC=dgwphotos,DC=local" }
    }
}}
$tweakedCSV | New-ADUser
Ryan Bolger
  • 16,755
  • 4
  • 42
  • 64
0

-OutVariable was added in more recent versions of powerhsell, and sometimes not all cmdLets support all those options. Try adding the following before the foreach line see if its being set "outvar count:" + $newusernames.count. If you do not see a number then -outvariable is not supported by the New-ADuser cmdlet. Fill the array another way like this: $newusernames = Import-Csv -Path $filepath

Clayton
  • 4,523
  • 17
  • 24
  • It is supported by New-ADuser, (that's why I'm using it) though it may not produce objects that Move-ADObject supports. However, I suspect that's not the case here. – Davidw Sep 17 '17 at 19:39