此脚本将创建新用户,但不会根据用户的描述移动他们?编辑,看来 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" }
}
}
答案1
您当前代码的结构意味着它必须先从 CSV 文件创建所有用户,然后才能将任何用户移动到其最终 OU。脚本真的完成了吗?还是当您没有看到用户进入其指定的 OU 时,您就过早停止了?
如果您希望在创建用户时移动他们,则应该Path
在 CSV 输出中包含参数,以便首先在目标 OU 中创建他们,或者重构代码以便在每次New-ADUser
调用后立即进行移动。
您还可以在调用之前调整 CSV 输出,New-ADUser
如下所示:
$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
答案2
-OutVariable
在较新版本的 powerhsell 中添加,有时并非所有 cmdLets 都支持所有这些选项。尝试在 foreach 行之前添加以下内容,查看是否已设置"outvar count:" + $newusernames.count
。如果您没有看到数字,则 cmdlet 不支持 -outvariable New-ADuser
。以另一种方式填充数组,如下所示:$newusernames = Import-Csv -Path $filepath