Script repository
The script creates a CSV file including information on password resets performed by users themselves. To execute the script, create a scheduled task configured for the Domain object type and add a managed domain to the Activity Scope of the task. The domain will only be used to trigger execution of the scheduled task.
Parameters
$csvFilePath- the full path to the CSV file that will be created by the script.$removeCsvFile- whether to remove the CSV file after sending.$to- a comma-separated list of recipients of the report.$subject- the email notification subject.$from- the e-mail address from which the notification will be sent.$mailServer- the SMTP server to be used when sending the report.$reportHeader- the email notification header.$reportFooter- the email notification footer.$operationStatusList- A template for the email notification part containing the total number of successful and failed operations. In the template, placeholder{0}will be replaced with the number of successful resets and{1}- failed.
$csvFilePath = "C:\Scripts\PasswordResetReport.csv" # TODO: modify me
$removeCsvFile = $True # TODO: modify me
# Email settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Self-Service Password Resets Report" # TODO: Modify me
$from = "noreply@localhost" # TODO: Modify me
$mailServer = "mail.domain.com" # TODO: Modify me
$reportHeader = "<h1><b>Self-Service Password Resets Report</b></h1><br/>"
$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
$operationStatusList = "
<ul>
<li>Successful password resets: {0}</li>
<li>Failed password resets: {1}</li>
</ul>" # TODO: Modify me
# Bind to the container for Password Self-Service statistics.
$passwordSelfServiceStatisticsPath = $Context.GetWellKnownContainerPath("PasswordSelfServiceStatistics")
$passwordSelfServiceStatistics = $Context.BindToObject($passwordSelfServiceStatisticsPath)
# Regenerate the Password Resets Report.
$passwordSelfServiceStatistics.ResetReportCache("ADM_PSSREPORTTYPE_RESETPWD")
$reportIsBeingGenerated = $True
# Get the report
do
{
try
{
$report = $passwordSelfServiceStatistics.GetReport("ADM_PSSREPORTTYPE_RESETPWD")
$reportIsBeingGenerated = $False
}
catch [System.Runtime.InteropServices.COMException]
{
if ($_.Exception.ErrorCode -eq "-2147024875")
{
# Report is being generated. Wait 10 seconds
Start-Sleep -Seconds 10
}
else
{
$reportIsBeingGenerated = $False
$Context.LogMessage($_.Exception.Message, "Error")
return
}
}
}
while ($reportIsBeingGenerated)
# Add the date when the report was generated.
$reportHeader = $reportHeader -f $report.GenerateDate
# Add the report records.
$records = $report.Records
$report = New-Object "System.Collections.ArrayList"
$successfullResetsCount = 0
$failedResetsCount = 0
for ($i = 0; $i -lt $records.Count; $i++)
{
$record = $records.GetRecord($i)
# Get user information
$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
# Get password self-service policy information.
$policyPath = $NULL
$policyName = $NULL
$policyInfo = $record.GetEnrollmentPolicyInfo([ref]$policyPath, [ref]$policyName)
# Create report entry.
$reportEntry = New-Object PSObject -Property @{
"Name" = $userDisplayName
"Parent" = $userParentCanonicalName
"Success" = $record.IsSuccessfull
"Policy" = $policyName
"Date/Time" = $eventDate
}
if ($record.IsSuccessfull)
{
$successfullResetsCount++
}
else
{
$failedResetsCount++
}
$report.Add($reportEntry)
}
$report | Export-Csv -Path $csvFilePath -NoTypeInformation
# Build html message
$operationStatusList = [System.String]::Format($operationStatusList, @($successfullResetsCount, $failedResetsCount))
$html = $reportHeader + $operationStatusList + $reportFooter
# Send message
Send-MailMessage -to $to -From $from -Body $html -BodyAsHtml -Attachments $csvFilePath -SmtpServer $mailServer -Subject $subject
if ($removeCsvFile)
{
# Remove CSV File
Remove-Item $csvFilePath -Force
}
Comments 0
You must be signed in to comment.