我很难修改我的脚本,在我提取的LDAP用户数据库中为名为Ismanager
的新字段添加YES或NO值。
由于无法判断用户是否是我们数据中的管理员,如果用户ID在managerldapuid
字段中出现的次数超过1次,则会插入YES值,如果不重复,则为NO
挑战在于如何在循环中插入查询来执行此比较任务,并将YES / NO值返回到最终数据提取。
Write-Verbose "Querying Enterprise Directory for uid list..."
$out = Search-Ldap "(&(uid=*)(objectclass=inetOrgPerson))" ("uid")
$totaluids = $out.entries.count
Write-VerboseWithMinutes ("Processing {0} uids..." -f $totaluids)
foreach ($uidentry in $out.entries) {
$loopcount++
$currentuid = $uidentry.attributes.uid[0]
if ($loopcount % 100 -eq 0) {
Write-VerboseWithMinutes (" {0} uids processed..." -f $loopcount)
}
$result = Search-Ldap "(&(uid=$($currentuid))(RequiresEthicsTraining=TRUE))" ("uid","givenname","sn","mail","jobfunction","facilityname","title","managerldapuid","co","c","division","hiredate","hrsystemid","hraccounttype","exemptstatus")
foreach ($entry in $result.entries) {
try {
manageremail = $null
$manageruid = $entry.attributes.managerldapuid[0]
if ($manageruid) {
$managerresult = Search-Ldap "(uid=$manageruid)" ("uid","mail")
$manageremail = $managerresult.entries[0].attributes.mail[0];
}
} catch {
$manageruid = $null
$manageremail = $null
}
$userdata += $entry |
select @{n='Employee ID';e={$_.attributes.uid[0]}},
@{n='Person ID';e={$_.attributes.hrsystemid[0]}},
@{n='First Name';e={$_.attributes.givenname[0]}},
@{n='Last Name';e={$_.attributes.sn[0]}},
@{n='Email';e={$_.attributes.mail[0]}},
@{n='Department';e={$_.attributes.jobfunction[0]}},
@{n='Ismanager';e={}},
}
}
Write-VerboseWithMinutes ("Finished processing")
$DataCsvName = "{1}\userdata-{0}.csv" -f ((get-date).tostring("yyyyMMdd")), $workdir
Write-VerboseWithMinutes ("Exporting {0} users to CSV" -f $userdata.Count)
$UserData | Export-CSV $DataCsvName -NoTypeInformation
}