Script repository
The script generates a report containing users that have a date stored in a text attribute older than specified in a parameter. To execute the script, create a report with a date/time parameter. The report must have a scope.
Parameters
$dateTimeFormat- the format of the date stored in the text attribute.$propertyName- the name of the text attribute storing the date.$daysParameterName- the name of the parameter used to select the number of days to deduct from the current date for comparison. The name must be specified with the param- prefix.
$dateTimeFormat = "dd/MM/yyyy" # TODO: modify me
$propertyName = "description" # TODO: modify me
$daysParameterName = "param-Days" # TODO: modify me
# Build comparison date.
$days = $Context.GetParameterValue($daysParameterName)
$threshold = (Get-Date).AddDays(- $days)
# Build criteria
$criteria = New-AdmCriteria "user"
$simpleItem = $criteria.CreateSimple()
$simpleItem.SetProperty($propertyName).SetComparisonOperator("empty").AddValue($False)
$criteria["user"].Add($simpleItem)
$Context.DirectorySearcher.AddCriteria($criteria)
$Context.DirectorySearcher.SearchParameters.PropertiesToLoad.Add($propertyName)
try
{
$searchIterator = $Context.DirectorySearcher.ExecuteSearch()
while ($Context.MoveNext($searchIterator))
{
$searchResult = $searchIterator.Current
try
{
$dateTime = $searchResult.GetPropertyByName($propertyName)
$date = [DateTime]::ParseExact($dateTime.Values[0], $dateTimeFormat, $NULL)
}
catch
{
continue
}
if($date -le $threshold)
{
$Context.Items.Add($searchResult)
}
}
}
finally
{
# Release resources
if ($searchIterator) { $searchIterator.Dispose() }
}
Comments 0
You must be signed in to comment.