我将数据库导出为CSV。在CSV中,包含employeeid,名字,姓氏,电子邮件,职位,描述和各种字段等字段。我试图将这些字段和所有其他字段填充到Active Directory中。提取的CSV字段不直接与Active Directory相关联。因此,我需要检查双重条目;如果有的话。
为此,我在CSV中获取用户的电子邮件地址,并在Active Directory中搜索电子邮件地址。然后我尝试使用相同的用户employeeid并检查是否填写了Active Directory employeeid属性。如果employeeid属性已填满,则跳过,如果没有,请填写CSV文件中的属性。
我无法将数据从CSV输入到Active Directory,也检查属性中是否存在employeeID。
Import-Module ActiveDirectory
# ERROR REPORTING ALL
Set-StrictMode -Version latest
$newpath = "C:\Scripts\Test Files\book1.csv"
$log = "C:\Scripts\Test Files\import_employeeID.log"
$date = Get-Date
$addn = (Get-ADDomain).DistinguishedName
$dnsroot = (Get-ADDomain).DNSRoot
$i = 1
function Start-Commands {
Update-Users
}
function Update-Users {
"Processing started (on " + $date + "): " | Out-File $log -Append
"---------------------------------------" | Out-File $log -Append
Import-CSV $newpath | ForEach-Object {
$UserEmail = $_.EMAILID
$EmployeeID = $_.EMPLID
$LastName = $_.LAST_NAME
$FIRSTNAME = $_.FIRST_NAME
$UserEmail
} -Properties EmployeeID
$CheckUser = $user.employeeid
if ($CheckUser) {
Write-Host "$UserEmail $EmployeeID UPDATED"
"$UserEmail $EmployeeID UPDATED `r" | Out-File $log -Append
} else {
Get-ADUser -Identity $User | Set-ADUser -EmployeeID $EmployeeID
Get-ADUser -Identity $User | Set-ADUser -Replace @{info=$EmployeeID}
}
$i++
}
"--------------------------------------------" + "`r`n" | Out-File $log -Append
}
Write-Host "STARTED SCRIPT`r`n"
Start-Commands
Write-Host "STOPPED SCRIPT"