我正在嘗試創建一個腳本,將我們的AD與CSV中的所有用戶進行比較。我們的人力資源部門爲所有員工提供主數據庫,但是當他們進行更改時,他們很少通知我們,因此他們現在將所有用戶從人力資源數據庫導出到CSV。將AD與CSV進行比較
我需要將此與我們的AD進行比較,並修改任何發現有變化的人或任何新員工。
我有下面的腳本,但它只是輸出所有員工,我只希望已更改的員工或未在AD中的新員工通過電子郵件發送。
write-host "Using default CSV file or C:\scripts\csv\StaffChanges.csv"
$StaffCSVUPath = "C:\scripts\csv\StaffChanges.csv"
$logfile = "C:\scripts\logs\ADvsCMIS.csv"
if(test-path $logfile) {
remove-item $logfile -force
}
function Email {
#Send an email, called with recipient email address and message body
param(
[string] $emailaddress="",
[string] $bodymsg=""
)
$bodymsg += "<p>"
$bodymsg += Get-Content($logfile)
Send-MailMessage -To $emailaddress -From "[email protected]" -Subject "(AD-CMIS_errors) Errors found between Active Directory and CMIS" -Body $bodymsg -BodyAsHTML -SMTPServer "exchserver"
}
function CheckOutputFile {
#Called with folder\filename and type of file
param(
[string]$outputfilename = "",
[string]$type = ""
)
if(test-path($outputfilename)) {
} else {
write-host "Creating $outputfilename"
$msg = "Forename,Surname,Username,ID"
$msg | out-file($outputfilename)
}
}
#Snap-ins needed to use the commands within the script
if((Get-pssnapin -Name Microsoft.Exchange.Management.Powershell.E2010 -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010}
if((Get-pssnapin -Name Quest.activeroles.admanagement -ErrorAction SilentlyContinue)-eq $null){Add-pssnapin Quest.activeroles.admanagement}
#import users from csv file
$users = (import-Csv $StaffCSVUpath)
$count=0
$countAD=0
Get-QADUser -searchroot "domain/Users/Staff" -SizeLimit 0 -includedproperties employeeid,displayname | ForEach-Object ($_.samaccountname) {
$found = 0
$countAD+=1
ForEach ($user in $users) {
$count+=1
$inital = $user.forename.substring(0,1)
$name = $user.forename+" "+$user.surname
$dispname = $inital+" "+$user.surname
if ($user.id -eq $_.employeeid) {
if ($user.surname -eq $_.lastname) {
if ($inital -eq $_.firstname) {
if ($name -eq $_.name) {
if ($dispname -eq $_.displayname) {
$found = 1
}
}
}
}
}
if ($found -eq 1){break}
}
if ($found -eq 0) {
if(($_.company -ne "testing") -band ($_.company -ne "service")) {
CheckOutputFile $logfile "LOG"
$msg = "<p>" + $_.firstname +" " + $_.lastname + " " + $_.samaccountname + " "+$_.employeeid +"<p>"
$msg | Out-File $logfile -append
}
}
}
if (test-path $logfile) {
#If there is anything to report
write-host "Emailing Log file to ict"
#Email file if $outputB exists
$email = "[email protected]"
$body = "Action Required: The users below do not exist within HR. Contact HR Data manager to resolve issue, delete users manually if required."
#email ict
Email $email $body
}
讓他們店的UPN在他們的基地,那麼你可以找到一個用戶通過其UPN快速。值得慶幸的是,UPN通常與電子郵件相同,儘管它可能並非如此。並且,當您處理敏感數據時需要警告,「修改」AD中的新員工可能會以授權用戶身份提供內部數據泄露。 – Vesper
只需簡要記錄,CSV文件只包含以下字段: - ID,名字,姓氏。該ID通常只是用戶名縮寫,但對每個用戶都是唯一的。 – lellis
如果您有辦法將ID映射到'sAMAccountName','userPrincipalName'或'cn',反之亦然,請按照CSV中的數據更新屬性。我說不要創建新用戶或刪除/禁用舊用戶。如果沒有,請設法做到這一點,如果有必要,您可以使用其他屬性。 – Vesper