【发布时间】:2016-11-11 14:45:08
【问题描述】:
我知道我可以使用 powershell 移动 AD 用户。我想要完成的是根据他们的描述移动一群用户。我有一个 csv 文件,在那个 csv 中,他们是一年的毕业专栏。我希望所有在 2016 年至 2022 年期间获得 YOG 的用户都转移到高中 OU。
我还没有尝试编写代码。我在 powershell 中成功地根据部门而不是描述来获取用户帐户。这是一些相同的数据
"ID","FNAME","LNAME","BDATE","GRD","SCHID"
"111111","TEst","student1","19980601","2016","1480"
"222222","test","Student2","19980522","2017","1480"
"333333","test","Student3","19970813","2025","1479"
我已将学校代码添加到 csv 文件中。我认为根据这个文件将学生移动到正确的 ou 会容易得多。 1480 是元素,1479 小时。这也是我用来创建 AD 帐户的代码。
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv userimport.csv
#Store report in log file in the $log variable
$log = "log.txt"
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Username = $User.ID
$Password = $User.BDATE
$Firstname = $User.FNAME
$Lastname = $User.LNAME
$Department = $User.GRD
$Company = $User.SCHID #This field refers to the OU the user account is to be moved to
#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, give a warning
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
"Processing started (on " + $date + "): " | Out-File $log -append
"--------------------------------------------" | Out-File $log -append
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@clasd.net" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Department "$Department" `
-Company "$Company" `
-EmailAddress "$Username@clasd.net" `
-Surname $Lastname `
-Enabled $True `
-Scriptpath "login.vbs" `
-DisplayName "$Firstname $Lastname" `
-Path "ou=users,ou=hs,dc=clasd,dc=net" `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
-ChangePasswordAtLogon $true
# Add User to Groups
#Add-ADPrincipalGroupMembership -Identity $Username -MemberOf "Elem","Elem Students"
Start-Sleep 3
# Move Users to appropiate OU based on School Code
$usr = import-csv userimport.csv
foreach ($User in $usr) {
if ($user.grd -in 2016){
Get-ADUser $User.ID | Move-ADObject -TargetPath 'OU=users,ou=hs,dc=clasd,dc=net'
}
}
}
}
【问题讨论】:
-
您好,能否提供一些您尝试编写的代码和一个示例 csv 文件?
-
我不担心同名学生。如果他们确实有相同的名字,我只需查看他们用于登录帐户的 ID 号。所有用户名都是他们的身份证号码。我只想在创建学生时将他们移动到正确的 OU 中。
-
AD 用户名的格式是什么?它们与 CSV 中的数据有何对应关系? YOG 是否已存储在 AD 属性中?
-
YOG 存储在 AD 用户的描述中。
标签: powershell active-directory