Hello,
The IAdmBusinessUnit::Members() method returns unmanaged user accounts if there are more than 1000 unmanaged user accounts. We'll fix the issue in the nearest minor release. Until the issue is fixed, you can use the following version of the script that uses the IAdmBusinessUnit::GetMemberGuids() to get Business Units. In this version of the script, the issue is not reproduced.
Thank you very much for the bugreport!
The script:
Import-Module Adaxes
# This exports multiple attributes and creates a CSV file minus the Header row.
# Also retrieves the common name for the positionPrimarySupervisor.
# Formats the user name as lastname , firstname middleInitial where possible, otherwise lastname , firstname
$sortBy = "employeeID" # sort search results by UID
$sortDirection = "Ascending" # sort in ascending order
$envir = "1" #Unrem for dev/test
#$envir = "2" #Unrem for production environment, and rem the line above
$businessUnitName = "For Export - Active Users (New)"
try
{
# Read the txt file that has the file export location and field information and then export data
$Context.LogMessage("About to export data to LD Service Desk","Information")
if ($envir -eq "1"){ #if dev/test environment
$filePath = "c:\AdaxesConfigurationFiles\(DEV)DirInt0002_Exp_Info.csv" #Read file from Adaxes c: drive that contain export information
} else { #if production environment
$filePath = "c:\AdaxesConfigurationFiles\DirInt0002_Exp_Info.csv" #Read file from Adaxes c: drive that contain export information
}
# Read the file and put into array variables
$ExpPath = @()
$LDAPNames = @()
$CSVNames = @()
Import-Csv $filePath |`
ForEach-Object {
$ExpPath += $_."ExportPath"
$LDAPNames += $_."LDAPName"
$CSVNames += $_."FieldsOut"
}
if($ExpPath.Length -eq 0)
{
$Context.LogMessage("No export path found. Processing terminated.", "Warning")
return
}
$Context.LogMessage("Export path is: " + $ExpPath, "Information")
$Context.LogMessage("LDAP Names are: " + $LDAPNames, "Information")
$Context.LogMessage("Output Fields: " + $CSVNames, "Information")
# Parse fields
$eachFieldIn = @($LDAPNames[0].split(","))
$eachFieldOut = @($CSVNames[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 if it already exists
if (Test-Path $exportFile)
{
Remove-Item $exportFile
}
# Create new file for the user report
New-Item $exportFile -Type File
# Find the Business Unit
$businessUnitsPath = $Context.GetWellKnownContainerPath("BusinessUnits")
$usersearcher = $Context.BindToObject($businessUnitsPath)
$usersearcher.SearchFilter = "(&(objectCategory=adm-BusinessUnit)(name=$businessUnitName))"
$usersearcher.PageSize = 500
$usersearcher.SearchScope = "ADS_SCOPE_SUBTREE"
<#
$userSearcher.SetPropertiesToLoad($eachFieldIn)
$sortOption = New-Object "Softerra.Adaxes.Adsi.AdmSortOption"
$sortOption.PropertyName = $sortBy
$sortOption.Direction = $sortDirection
$userSearcher.Sort = $sortOption
#>
# Get the user information from the search results add them to the file
try
{
$userResult = $userSearcher.ExecuteSearch()
$objects = $userResult.FetchAll()
if ($objects.Length -gt 1)
{
$Context.LogMessage("Found more than one Business Unit with name '$businessUnitName'.", "Warning")
return
}
if ($objects.Length -eq 0)
{
$Context.LogMessage("Business Unit '$businessUnitName' does not exist.", "Error")
return
}
# Get the Business Unit Members
$unit = $Context.BindToObject($objects[0].AdsPath)
$members = $unit.Members()
$totalUserCount = $members.Count
#$Context.LogMessage("The total number of users is: " + $count, "Information")
$count = 0
$report = @()
$configurationSetSettingsPath = $Context.GetWellKnownContainerPath("ConfigurationSetSettings")
$admConfigurationSetSettings = $Context.BindToObject($configurationSetSettingsPath)
for ($i = 0; $i -lt $totalUserCount; $i++)
{
# Check whether the user is managed by Adaxes
$userRec = $members.GetObject($i)
$userSidsBytes = $userRec.Get("ObjectSid")
$sid = New-Object "Softerra.Adaxes.Adsi.Sid" @($userSidsBytes, 0)
if ($admConfigurationSetSettings.IsUnmanagedAccount($sid))
{
continue
}
$reportRecord = New-Object PSObject
foreach ($propertyName in $eachFieldIn)
{
try
{
switch ($propertyName)
{
"positionPrimarySupervisor" {
try {
$managerDN = $userRec.Get($propertyName)
try
{
$supervisor = $Context.BindToObjectByDn($managerDN)
$value = $supervisor.Get("cn")
}
catch
{
$value = ""
}
}
catch {
$value = ""
}
}
"cn" {
# format name as lastname , firstname middleinitial if possible, otherwise as lastname , firstname
$lname = $userRec.Get("sn")
try {
$fname = $userRec.Get("preferredFirstName")
}
catch {
$fname = $userRec.Get("givenName")
}
try {
$mname = $userRec.Get("middleName")
}
catch {
$mname = ""
}
$value = $lname + " , " + $fname + " " + $mname
}
Default {
$value = $userRec.Get($propertyName)
}
} # end switch $propertyName
}
catch
{
$value = "" # The property is empty
}
$reportRecord | Add-Member -Name $propertyName -Value $value -MemberType NoteProperty
} # end foreach propertyname
$report += $reportRecord
$count++
} # end foreach user
}
finally
{
$userResult.Dispose()
}
#$report | Export-Csv $exportFile -NoTypeInformation *** Uncomment this line to export with the Header row
$report | ConvertTo-Csv -NoTypeInformation | select -Skip 1 | Set-Content $exportFile
$Context.LogMessage("Total records exported = " + $count, "Information")
}
catch #catch any unresolved errors
{
$ErrorMessage = $_.Exception.Message
[string]$LineNumber = $_.InvocationInfo.ScriptLineNumber
[string]$Offset = $_.InvocationInfo.OffsetInLine
[string]$errLine = $_.InvocationInfo.Line
Write-Error $ErrorMessage
$Context.LogMessage("At Line #: " + $LineNumber + " at char " + $Offset, "Error")
$Context.LogMessage("Executing: " + $errLine, "Error")
}