We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Users disabled during a number of days

September 23, 2024 Views: 751

The script generates a report containing user accounts disabled during the specified number of days. For information on creating reports, see the Create report tutorial.

In the script, the $daysParamNumber specifies the name of the report parameter used to select the number of days. The parameter can be of the Drop-down (item values should contain the number of days to check for) or Edit box (only integer values allowed) type. The parameter name should be specified with the param- prefix.

Edit Remove
PowerShell
$daysParamNumber = "param-MyParam" # TODO: modify me

$days = $Context.GetParameterValue($daysParamNumber)

# Bind to the directory object representing the General Log
$path = $Context.GetWellKnownContainerPath("ServiceLog")
$serviceLog = $Context.BindToObject($path)

$generalLog = $serviceLog.GeneralLog
if ($days -ne 0)
{
   $generalLog.StartDateTime = (Get-Date).AddDays(-$days)
   $generalLog.EndDateTime = Get-Date
}

# Get the log records
$log = $generalLog.Log
$records = $log.GetPage(0)

# Build filter to search for disabled users
$guidComparer = $Context.CreatePropertyValueComparer("objectGuid")
$guids = New-Object System.Collections.Generic.HashSet[byte[]] @($guidComparer)

foreach ($record in $records)
{
    if ($Context.Items.Aborted)
    {
        return
    }
    
    if (($record.TargetObjectType -ne "user") -or ($record.TargetObjectGuid -eq $NULL))
    {
        continue
    }
    
    $operationTypes = $record.GetOperationTypes()
    if ($operationTypes -notcontains "disable account")
    {
        continue
    }

    # Get GUID
    $guidBytes = ([Guid]$record.TargetObjectGuid).ToByteArray()
    $guids.Add($guidBytes)
}

$searcher = $Context.CreateGuidBasedSearcher(@($guids))
$searcher.Criteria = New-AdmCriteria -Type "user" -Expression {accountDisabled -eq $true}
$Context.Items.Add($searcher)
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers