Script Repository


Users with date stored in text attribute older than selected

October 04, 2021
59

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 the corresponding parameter. The scope for report generation should be set to an AD location.

Parameters:

  • $dateTimeFormat - Specifies the format of the date as it is stored in the text attribute.
  • $propertyName - Specifies the LDAP name of the text attribute storing the date.
  • $daysParameterName - Specifies the name of the parameter used to select the number of day to deduct from the current date for comparison. The parameter name must be specified with the param- prefix.
Edit Remove
PowerShell
$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)

# Append the search filter
$Context.DirectorySearcher.AppendFilter("(&(sAMAccountType=805306368)($propertyName=*))")
$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 )
No results found.
Leave a comment