Hi,
I am currently trying to create this report:
- Enter a startDate
- Enter a endDate
- Select value (A, B, Both)
No I need to find all users
- if A is selcted
- all Users where A is within range of startDate and endDate
- if B is selcted
- all Users where B is within range of startDate and endDate
- if Both is selcted
- all Users where A OR B is within range of startDate and endDate
A and B has a format like this in AD
string_with_info_YYYY-MM-DD
This is my current approach
# Retrieve the type of search (A, B, or Both)
$searchType = $Context.GetParameterValue("param-searchType")
# Retrieve the start and end date
$startDate = $Context.GetParameterValue("param-startDate")
$endDate = $Context.GetParameterValue("param-endDate")
# Convert start and end dates to YYYY-MM-DD format for comparison
$startDateFormatted = [DateTime]::Parse($startDate).ToString("yyyy-MM-dd")
$endDateFormatted = [DateTime]::Parse($endDate).ToString("yyyy-MM-dd")
# Validate that the dates are in the correct format
if (-not $startDate -or -not $endDate) {
throw "Both start and end dates must be specified."
}
# Initialize an LDAP filter to retrieve all users with either A or B attribute set
$ldapFilter = "(&(objectClass=user)(|(A=*)(B=*)))"
# Apply the filter to the directory searcher
$Context.DirectorySearcher.AppendFilter($ldapFilter)
# Load the required properties during the search
$Context.DirectorySearcher.SearchParameters.PropertiesToLoad.AddRange(@("A", "B"))
# Execute the search and filter users by date in PowerShell after retrieving the results
try {
$searchIterator = $Context.DirectorySearcher.ExecuteSearch()
while ($Context.MoveNext($searchIterator)) {
$searchResult = $searchIterator.Current
# Retrieve the value from the search result properties
$A = if ($searchResult.Properties.Contains("A")) { [string]$searchResult.Properties["A"][0] } else { $null }
$B = if ($searchResult.Properties.Contains("B")) { [string]$searchResult.Properties["B"][0] } else { $null }
# Extract the date part from A and B strings
if ($A) {
$ADate = $A.Substring($A.Length - 10) # Extract the last 10 characters (YYYY-MM-DD)
}
if ($B) {
$BDate = $B.Substring($B.Length - 10) # Extract the last 10 characters (YYYY-MM-DD)
}
# Perform the comparison based on the search type
if ($searchType -eq "A" -and $ADate -ge $startDateFormatted -and $ADate -le $endDateFormatted) {
$Context.Items.Add($searchResult)
} elseif ($searchType -eq "B" -and $BDate -ge $startDateFormatted -and $BDate -le $endDateFormatted) {
$Context.Items.Add($searchResult)
} elseif ($searchType -eq "Both" -and (($ADate -ge $startDateFormatted -and $ADate -le $endDateFormatted) -or ($BDate -ge $startDateFormatted -and $BDate -le $endDateFormatted))) {
$Context.Items.Add($searchResult)
}
}
} finally {
if ($searchIterator) { $searchIterator.Dispose() }
}
However, no result is shown.
I am not sure if I get the correct value of each found user for A and B to compare it