I've got an Adaxes scheduled task that will export records out of AD into a JSON text file. All is working well except for the ability to sort the objects returned from DirectorySearcher function. The code doesn't fail, it just doesn't sort by employeeID as expected. Can you give me an idea of what I'm missing. The code is attached below.
Import-Module Adaxes
# Formats output as JSON
function ConvertTo-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
return $ps_js.Serialize($item)
}
# Read the txt file that has the file export location and field information and then export data to Sharepoint
$Context.LogMessage("About to export data to Sharepoint","Information")
$filePath = "c:\DirInt0118_Exp_Info.csv" #Read file from Adaxes c: drive that contain export information
# Read the file and put into array variables
$ExpPath = @()
$LDAPNames = @()
$JSONNames = @()
Import-Csv $filePath |`
ForEach-Object {
$ExpPath += $_."ExportPath"
$LDAPNames += $_."LDAPName"
$JSONNames += $_."FieldsOut"
}
if($ExpPath.Length -eq 0)
{
$Context.LogMessage("No data found to import", "Information")
return
}
$Context.LogMessage("Export path is: " + $ExpPath, "Information")
$Context.LogMessage("LDAP Names are: " + $LDAPNames, "Information")
$Context.LogMessage("Output Fields labels are: " + $JSONNames, "Information")
# Parse fields
$eachFieldIn = @($LDAPNames[0].split(","))
$eachFieldOut = @($JSONNames[0].split(","))
if (($eachFieldIn.Length -eq 0) -or ($eachFieldOut.Length -eq 0))
{
$Context.LogMessage("Fields missing during import. Processing terminated.", "Warning")
return
}
if (($eachFieldIn.Length) -ne ($eachFieldOut.Length))
{
$Context.LogMessage("Field in/out count mismatch during import. Processing terminated.", "Warning")
return
}
# Build path for the user report file
$exportFile = $ExpPath[0]
# Delete the existing file
Remove-Item $exportFile
# Create new file for the user report
New-Item $exportFile -Type File
# Get AdsPath of target object
$targetObject = $Context.BindToObjectByDN("%distinguishedName%")
$targetObjectPath = $targetObject.ADsPath
$Context.LogMessage("Domain being searched is: " + $targetObjectPath, "Information")
# Search all users in the target OU
$userSearcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
$userSearcher.SearchParameters.BaseObjectPath = $targetObjectPath
$userSearcher.SearchParameters.PageSize = 1000
$userSearcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
# Filter for User objects that are not disabled and employeeID doesn't contain zeros or 'NMU' or employeeID is not blank
$userSearcher.SearchParameters.Filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!(employeeID=000000))(!(employeeID=NMU))(employeeID=*))"
$userSearcher.SetPropertiesToLoad($eachFieldIn)
$userResult = $userSearcher.ExecuteSearch()
# Sort by employeeID
[color=#BF0000]$sortedUsers = $userResult.FetchAll() | sort-Object $_.properties.employeeID
[/color]
$totalUserCount = $sortedUsers.Count
$Context.LogMessage("The number of users is: " + $totalUserCount, "Information")
$count = 0
# Get the user information from the search results, convert to JSON and add them to the file
# Write out opening bracket
Add-Content $exportFile "["
foreach ($user in $sortedUsers)
{
$hash1 = @{}
for ($i=0; $i -lt $eachFieldIn.length; $i++)
{
$fldValue = $user.Properties[$eachFieldIn[$i]].Value
$hash1.Add($eachFieldOut[$i] , $fldValue)
}
$result = ConvertTo-Json20($hash1)
$count++
# If it's not the last record, add a comma to the end of the content, otherwise, leave it off
if ($count -ne $totalUserCount) {
Add-Content $exportFile "`n$result,"
} else {
Add-Content $exportFile "`n$result"
}
}
$userResult.Dispose()
# Write out closing bracket
Add-Content $exportFile "`n]"
$Context.LogMessage("Total records written = " + $count, "Information")