@foggy i @Fico
Hej ekipa, evo detaljnije objasnjena:
POLJE 1
Email
Date
POLJE 1 ENRICHED IZ AD-a
Date
SamAccountName
RealName
Office
Email
POLJE 2
ControllerId
EventTime
Username
UserToken
EventId
Znaci tek kada "obogatim" POLJE 1 sa podacima iz ADa, mogu raditi usporedbu po SamAccountNameu (username):
foreach ($Entry in $POLJE1) {
if ($POLJE2.username -notcontains $Entry.SamAccountName) {
$Difference += $Entry.Email
}
}
Problem je upravo to "obogacivanje" POLJA1, gdje koristenje "where-object", "contains" ili bilo cega drugog traje predugo, jer je AD uz sve filtere prevelik i to vrti jako dugo.
Ja sam sam tu potrebu koristio Binary Search algoritam:
function BinarySearch {
Param ($SortedArray, $Attribute, $SearchedObject)
$startIndex = 0
$endIndex = $SortedArray.length - 1
$Output = @()
if ( ( $SortedArray[$startIndex].$Attribute -gt $SearchedObject ) -or ($SortedArray[$endIndex].$Attribute -lt $SearchedObject ) ) {
return "Out of scope!"
}
while ($startIndex -le $endIndex) {
$middleIndex = $startIndex + [Math]::floor(($endIndex - $startIndex) / 2)
if ($SortedArray[$middleIndex].$Attribute -eq $SearchedObject) {
$Output += $SortedArray[$middleIndex]
for ($n=$middleIndex - 1;$SortedArray[$n].$Attribute -eq $SearchedObject;$n--) {
$Output += $SortedArray[$n]
}
for ($n=$middleIndex + 1;$SortedArray[$n].$Attribute -eq $SearchedObject;$n++) {
$Output += $SortedArray[$n]
}
}
if ($SortedArray[$middleIndex].$Attribute -lt $SearchedObject) {
$startIndex = $middleIndex + 1
}
else {
$endIndex = $middleIndex - 1
}
}
return $Output
}
Medutim, da bi binary radio, on mora operirati sa sortiranim poljem, a upravo to sortiranje u Powershellu ne radi kako spada:
UPOTREBA BINARY SEARCH da bi se obogatilo POLJE1 sa podacima iz ADa:
foreach ($Entry in $POLJE1) {
$UserEntry = BinarySearch -SortedArray $ADExport -Attribute Mail -SearchedObject $Entry.Email
AD Export i sortiranje PWS cmdletom "Sort-Object":
$ADExport = Get-ADUser -Filter 'Mail -like "XXX" -and Enabled -eq "True"' -SearchBase $Path -Properties Mail, extensionAttribute8 | Select-Object SamAccountName, Mail, Name | Sort-Object -Property Mail
Ono sto meni treba je Quicksort funkcija koja operira nad ADExportom i poslozi po propertyu (kolumni) Mail, ali isto to napravi nad propertyima SamAccountName, Name. Ovo je kako trenutno izgleda moja quicksort funkcija:
function quicksort($array) {
$less, $equal, $greater = @(), @(), @()
if( $array.Count -gt 1 ) {
$pivot = $array[0]
foreach( $x in $array) {
if($x -lt $pivot) { $less += @($x) }
elseif ($x -eq $pivot) { $equal += @($x)}
else { $greater += @($x) }
}
$array = (@(quicksort $less) + @($equal) + @(quicksort $greater))
}
$array
}