基本上,将处理传递到脚本中的任何数据集,然后如果用户不存在则将创建用户。如果用户已存在于AD中,则脚本会跳过该条目。这是一个仅限CREATE的脚本。
这很慢,我想在保持功能的同时提高性能。你能否告诉我如何使这项表现更好?
import-csv "c:\PSScripts\LDAP\ADMigrate.csv" | ForEach-Object {
# Define the User OU
$usersOU = [ADSI] "LDAP://ou=Students, dc=live,dc=tcicollege,dc=edu"
# Check for existing users
$existingUsers = ($usersOU.psbase.children | Where-Object {$_.psBase.schemaClassName -eq "User"} | Select-Object -expand Name)
$userQuery = $existingUsers -contains $_.'AccountName'
if ($userQuery) {
echo $_.'AccountName' " already exists in Directory."
} else {
# Create a new user
$newUser = $usersOU.create("user","cn=" + $_.'AccountName')
# Set Account AttributesAMAccountName
$newUser.Put("sAMAccountName", $_.'AccountName')
$newUser.Put("givenName", $_.'FirstName')
$newUser.Put("employeeID", $_.'StudentID')
$newUser.Put("sn", $_.'LastName')
$newUser.Put("department", $_.'Department')
$newUser.Put("company", $_.'SyStudentID')
$newUser.Put("UserPrincipalName", $_.'AccountName' + "@live.tcicollege.edu")
$newUser.Put("mail", $_.'AccountName' + "@live.tcicollege.edu")
$newUser.Put("displayName", $_.'LastName' + "," + " " + $_.'FirstName')
# First Commit
$newUser.SetInfo()
$newUser.userAccountControl="66048"
$newUser.Put("pwdLastset", -1)
$newUser.SetPassword($_.'Password')
# Final Commit
$newUser.SetInfo()
echo $_.'AccountName' " created successfully."
}
}
提前感谢您提供的任何帮助。
答案 0 :(得分:5)
尝试使用静态Exists()方法查找学生OU中是否存在用户:
$user = [ADSI]::Exists("LDAP://cn=$($_.AccountName),ou=Students, dc=live,dc=tcicollege,dc=edu")
if(!$user)
{
"create code goes here"
}
$ usersOU值是静态的,因此您可以将其取出,将其放在import-csv命令之前。