Script Repository


Created, moved and deleted Organizational Units

February 24, 2021
1264

The script creates a report on Organizational Units created, moved or deleted via Adaxes.

To generate such a report upon request, you can create a custom command that runs the script. To schedule the reports, you need to create a scheduled task configured for the Domain-DNS object type and run it against any of your AD domains.

To add the script to a custom command or scheduled task, use the Run a program or PowerShell script action.

Parameters:

  • $numDays - Specifies the number of days to include in the report. Set to 0 to include the whole history.
  • $to - Specifies email addresses of the recipient(s) of the report.
  • $subjectTemplate - Specifies a template for the email message subject.
  • $reportHeaderTemplate - Specifies a template for the email message header.
  • $tableTemplate - Specifies a template for the report table header.
  • $reportFooter - Specifies the email message footer.
Edit Remove
PowerShell
$numDays = 1 # set to 0 to output all records
$to = "recipient@domain.com" # TODO: modify me
$subjectTemplate = "Organizational Units created, moved or deleted {0}" # TODO: modify me
$reportHeaderTemplate = "<b>Organizational Units created, moved or deleted {0}</b><br/><br/>"
$tableTamplate = "
<b>{0}</b>
<table border='1' style='width:100%%'>
  <col width='40%%'>
  <col span=2 width='30%%'>
    <tr>
        <th>Name</th>
        <th>Initiator</th>
        <th>Date and Time</th>
    </tr>
" # 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 Service General Log
$path = $Context.GetWellKnownContainerPath("ServiceLog")
$serviceLog = $Context.BindToObject($path)

# Set start and end dates
$generalLog = $serviceLog.GeneralLog
if ($numDays -ne 0)
{
    $generalLog.StartDateTime = (Get-Date).AddDays(-$numDays)
    $generalLog.EndDateTime = Get-Date
    $reportHeader = [System.String]::Format($reportHeaderTemplate, "during $numDays day(s)")
    $subject = [System.String]::Format($subjectTemplate, "during $numDays day(s)")
}
else
{
    $reportHeader = [System.String]::Format($reportHeaderTemplate, "through all history")
    $subject = [System.String]::Format($subjectTemplate, "through all history")
}

# Get log records
$log = $generalLog.Log
$records = $log.GetPage(0) | Sort-Object -Property "TargetObjectName"

$tableMoveOperation = [System.String]::Format($tableTamplate, "Move")
$recordsMoveOperation = [System.String]::Empty
$tableDeleteOperation = [System.String]::Format($tableTamplate, "Delete")
$recordsDeleteOperation = [System.String]::Empty
$tableCreateOperation = [System.String]::Format($tableTamplate, "Create")
$recordsCreateOperation = [System.String]::Empty
foreach ($record in $records)
{
    # Check target object type
    if  ($record.TargetObjectType -ne "organizationalUnit")
    {
        continue
    }
    
    # Check operations
    $operations = $record.GetOperationTypes()
    $targetObjectGuid = [Guid]$record.TargetObjectGuid
    $initiator = $record.Initiator.Name
    $completionTime = $record.CompletionTime
    try
    {
        $targetObject = $Context.BindToObject("Adaxes://<GUID=$targetObjectGuid>")
        $targetObjectName = $Context.GetDisplayNameFromAdsPath($targetObject.AdsPath)
    }
    catch
    {
        $targetObjectName = $record.TargetObjectName
    }
    
    if ($operations -contains "move")
    {
        $recordsMoveOperation += "<tr><td>$targetObjectName</td><td>$initiator</td><td>$completionTime</td></tr>"
    }
    elseif ($operations -contains "delete")
    {
        $recordsDeleteOperation += "<tr><td>$targetObjectName</td><td>$initiator</td><td>$completionTime</td></tr>"
    }
    elseif ($operations -contains "create")
    {
        $recordsCreateOperation += "<tr><td>$targetObjectName</td><td>$initiator</td><td>$completionTime</td></tr>"
    }
}

# Build report
$html = $reportHeader
if (!([System.String]::IsNullOrEmpty($recordsCreateOperation)))
{
    $html += "$tableCreateOperation$recordsCreateOperation</table><br/>"
}

if (!([System.String]::IsNullOrEmpty($recordsDeleteOperation)))
{
    $html += "$tableDeleteOperation$recordsDeleteOperation</table><br/>"
}

if (!([System.String]::IsNullOrEmpty($recordsMoveOperation)))
{
    $html += "$tableMoveOperation$recordsMoveOperation</table><br/>"
}
$html += $reportFooter

# Send mail
$Context.SendMail($to, $subject, $NULL, $html)


Comments ( 0 )
No results found.
Leave a comment