Powershell脚本创build新用户并根据用户描述将它们移到OU中将无法移动用户

这个脚本会创build新的用户,但是不会根据用户的描述来移动它们。 编辑看来,Powershell显示的描述字段为空白,但它填充在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" } } } 

目前你的代码结构的方式意味着它必须在CSV文件中创build所有的用户,然后将其移动到最终的OU。 脚本真的完成了吗? 还是当你没有看到用户进入他们指定的OU时,你是否提前停下来?

如果您希望在创build用户时移动用户,则应该将Path参数包含在CSV输出中,以便在目标OU中创build它们以便开始或重新构build代码,以便在每个New-ADUser电话。

在调用New-ADUser之前,你也可以调整你的CSV输出:

 $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 

-OutVariable是在更新版本的powerhsell中添加的,有时并不是所有的cmdLets都支持所有这些选项。 尝试在foreach行之前添加以下内容,看看它是否被设置为"outvar count:" + $newusernames.count 。 如果您没有看到数字,则New-ADuser cmdlet不支持New-ADuser 。 像这样填充数组: $newusernames = Import-Csv -Path $filepath