Script Repository

Users enrolled under currently not effective policy

February 24, 2021

The script generates and emails a report on users enrolled under a currently not effective Password Self-Service Policy and also not enrolled users.

Note: To schedule the report, create a scheduled task configured for the Domain-DNS object type that runs the script and assign it over any of your AD domains. To add the script to a scheduled task, use the Run a program or PowerShell script action.


  • $to - Specifies a comma separated list of recipients of the report.
  • $subject - Specifies the email message subject.
  • $reportHeader - Specifies the email message header. In the header, the {0} placeholder will be replaced with the date when the report was generated.
  • $reportFooter - Specifies the email message footer.
Edit Remove
$to = "" # TODO: modify me
$subject = "Users Enrolled Under Currently Not Effective Policy and Not Enrolled Users" # TODO: modify me
$reportHeader = "<b>Users Enrolled Under Currently Not Effective Policy and Not Enrolled Users. Report generated on: {0} </b><br/><br/>" # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

# Bind to the container for Password Self-Service Statistics
$passwordSelfServiceStatisticsPath = $Context.GetWellKnownContainerPath("PasswordSelfServiceStatistics")
$passwordSelfServiceStatistics = $Context.BindToObject($passwordSelfServiceStatisticsPath)

# Regenerate the Enrollment Report
$reportIsBeingGenerated = $True

# Get the Report
        $report = $passwordSelfServiceStatistics.GetReport("ADM_PSSREPORTTYPE_ENROLLMENT")
        $reportIsBeingGenerated = $False
    catch [System.Runtime.InteropServices.COMException]
        if ($_.Exception.ErrorCode -eq "-2147024875")
            # Report is being generated. Wait 5 seconds
            Start-Sleep -Seconds 5
            $reportIsBeingGenerated = $False
            $Context.LogMessage($_.Exception.Message, "Error")
while ($reportIsBeingGenerated)

# Add the date when the report was generated
$reportHeader = $reportHeader -f $report.GenerateDate

# Build report records
$records = $report.Records
$enrolledAccounts = New-Object "System.Text.StringBuilder"
$notEnrolledAccounts = New-Object "System.Text.StringBuilder"

for ($i = 0; $i -lt $records.Count; $i++)
    $record = $records.GetRecord($i)
    # Get user info
    $userPath = $NULL
    $userDisplayName = $NULL
    $userParentCanonicalName = $NULL
    $userAccountIsEnabled = $NULL
    $userIsEnrolled = $NULL
    $userAccountIsExpired = $NULL
    $userInfo = $record.GetUserInfo([ref]$userPath, [ref]$userDisplayName, [ref]$userParentCanonicalName, 
        [ref]$userAccountIsEnabled, [ref]$userIsEnrolled, [ref]$userAccountIsExpired)
    $eventDate = $record.EventDate

    if ($eventDate -eq [DateTime]::MinValue)
        $eventDate = $NULL
    # Get enrollment policy information
    $policyPath = $NULL
    $policyName = $NULL
    $record.GetEnrollmentPolicyInfo([ref]$policyPath, [ref]$policyName)
    # Get effective policy information
    $effectivePolicyPath = $NULL
    $effectivePolicyName = $NULL
    $record.GetEffectivePolicyInfo([ref]$effectivePolicyPath, [ref]$effectivePolicyName)

    if ($userIsEnrolled -and 
        ($policyPath -ne $effectivePolicyPath))
        [void]$enrolledAccounts.Append("<td>$userDisplayName ($userParentCanonicalName)</td>")
    elseif (-not($userIsEnrolled) -and 
        [void]$notEnrolledAccounts.Append("<td>$userDisplayName ($userParentCanonicalName)</td>")
        [void]$notEnrolledAccounts.Append("<td>$effectivePolicyName </td>")
        [void]$notEnrolledAccounts.Append("<td>Not Enrolled</td>")

# Build html report
$html = New-Object "System.Text.StringBuilder"
if ($enrolledAccounts.Length -eq 0 -and
    $notEnrolledAccounts.Length -eq 0)
    [void]$html.Append("<b>No users found</b>")
    [void]$html.Append("<table border='1'>")
    [void]$html.Append("<th>Effective Policy</th>")
    [void]$html.Append("<th>Enrollment Policy</th>")
    if ($enrolledAccounts.Length -ne 0)
    if ($notEnrolledAccounts.Length -ne 0)

# Send the report
$Context.SendMail($to, $subject, $NULL, $html.ToString())

Comments ( 0 )
No results found.
Leave a comment