Script Repository


Email information on each approved request to perform operations of a certain type

May 18, 2020
904

The script emails information on each request to perform operations of a certain type (e.g. Modify). The email includes the requestor, the user who approved it and a list of all properties of the new object. To use the script with Adaxes, create a Business Rule that runs the script after modifying an ApprovalRequest object.

Parameters:

  • $subject - specifies the subject of the email notification sent by the script;
  • $to - specifies recipients of the email notifications;
  • $operationTypes - specifies a list of operations that will trigger the emails;
  • $operationTypesToSkip - specifies a list of operations for which emails will not be triggered. Use this parameter for approvals that contain 2 or more operation types. For example an Approval Request to disable a user account will contain 2 operation types: set properties and disable account. An Approval Request to reset a password will contain 3 operation types: set properties, reset password, and modify password.
    For a list of possible values for $operationTypes and $operationTypesToSkip, see the table in Managing Business Rules.
Edit Remove
PowerShell
$subject = "Object Modified" # TODO: modify me
$to = "administrator@example.com" # TODO: modify me
$operationTypes = @("set properties") # TODO: modify me
$operationTypesToSkip = @("disable account", "reset password") # TODO: modify me

function BuildOperationDescriptionHtml ($xmlReader, $htmlDescriptionBuilder, $xmlEncoder)
{
    $namespaceURI = "http://softerra.com/adaxes"
    $colorForTagName = @{
        "objectName" = "#A31515";
        "objectType" = "#1E50FF";
        "propertyName" = "#002D00";
        "propertyValue" = "#1E50FF";
        "regexp" = "#E700E7"
    }

    $xmlReader.MoveToContent()
    $xmlReader.ReadStartElement("message", $namespaceURI)
    $htmlDescriptionBuilder.Append("<div>") | Out-Null

    while (!$xmlReader.EOF)
    {
        if (($xmlReader.NodeType -eq [System.Xml.XmlNodeType]::EndElement) -and
            ($xmlReader.NamespaceURI -eq $namespaceURI))
        {
            if ($xmlReader.LocalName -eq "message")
            {
                $htmlDescriptionBuilder.Append("</div>") | Out-Null
                break
            }
            elseif ($xmlReader.LocalName -eq "additionalData")
            {
                $htmlDescriptionBuilder.Append("</div>") | Out-Null
                $xmlReader.ReadEndElement()
            }
            elseif ($xmlReader.LocalName -eq "additionalDataGroup")
            {
                $htmlDescriptionBuilder.Append("</div></div>") | Out-Null
                $xmlReader.ReadEndElement()
            }
        }
        elseif ($xmlReader.NodeType -eq [System.Xml.XmlNodeType]::Element)
        {
            if ($xmlReader.LocalName -eq "separator")
            {
                $xmlReader.ReadStartElement()
                $htmlDescriptionBuilder.Append(",<br/>") | Out-Null
            }
            elseif ($xmlReader.LocalName -eq "additionalData")
            {
                $xmlReader.ReadStartElement()
                $htmlDescriptionBuilder.Append('<div style="margin-left:1em">') | Out-Null
            }
            elseif ($xmlReader.LocalName -eq "additionalDataGroup")
            {
                $groupTitle = $xmlReader["title"]
                $xmlReader.ReadStartElement()
                $htmlDescriptionBuilder.Append("<div>") | Out-Null
                $htmlDescriptionBuilder.Append("<div>") | Out-Null
                $htmlDescriptionBuilder.Append($xmlEncoder.XmlElementTextEncode($groupTitle)) | Out-Null
                $htmlDescriptionBuilder.Append("</div>") | Out-Null
                $htmlDescriptionBuilder.Append('<div style="margin-left:1em">') | Out-Null
            }
            else
            {
                $color = $colorForTagName[$xmlReader.LocalName]
                if (![System.String]::IsNullOrEmpty($color))
                {
                    $htmlDescriptionBuilder.AppendFormat('<span style="color:{0}">', $color) | Out-Null
                }
                else
                {
                    $htmlDescriptionBuilder.Append("<span>") | Out-Null
                }

                $htmlDescriptionBuilder.Append($xmlEncoder.XmlElementTextEncode($xmlReader.ReadElementString())) | Out-Null
                $htmlDescriptionBuilder.Append("</span>") | Out-Null
            }
            continue
        }
        elseif ($xmlReader.NodeType -eq [System.Xml.XmlNodeType]::Text)
        {
            $htmlDescriptionBuilder.Append($xmlEncoder.XmlElementTextEncode($xmlReader.ReadString())) | Out-Null
        }
        else
        {
            $xmlReader.Read()
        }
    }
    $xmlReader.ReadEndElement()
}

# Check approval state
if ($Context.TargetObject.ApprovalState -ne "ADM_APPROVALSTATE_APPROVED")
{
    return
}

# Check operation type
if ($operationTypes -ne $NULL)
{
    $operations = $Context.TargetObject.TypeNamesOfOperationToApprove
    $result = Compare-Object -ReferenceObject $operations -DifferenceObject $operationTypes -IncludeEqual -ExcludeDifferent
    if ($result -eq $NULL)
    {
        return
    }
}

if ($operationTypesToSkip -ne $NULL)
{
    $result2 = Compare-Object -ReferenceObject $operations -DifferenceObject $operationTypesToSkip -IncludeEqual -ExcludeDifferent
    if ($result2 -ne $NULL)
    {
        return
    }
}

# Build report
$htmlBuilder = New-Object "System.Text.StringBuilder"
$htmlBuilder.Append('<table border="0">') | Out-Null

# Add operation description to the report
$settings = New-Object "System.Xml.XmlReaderSettings"
$settings.IgnoreComments = $true;
$settings.IgnoreWhitespace = $false;
$settings.IgnoreProcessingInstructions = $true;
$settings.CheckCharacters = $false;

$stringReader = New-Object "System.IO.StringReader" $Context.TargetObject.DescriptionOfOperationToApproveXml
try
{
    $xmlReader = [System.Xml.XmlReader]::Create($stringReader, $settings)
    $xmlEncoder = New-Object "Softerra.Adaxes.Utils.XmlEncoder"
    $htmlBuilder.Append("<tr><td>Operation</td><td>") | Out-Null
    BuildOperationDescriptionHtml $xmlReader $htmlBuilder $xmlEncoder
    $htmlBuilder.Append("</td></tr>") | Out-Null
}
finally
{
    # Release resources
    $xmlEncoder.Close()
    $xmlReader.Close()
    $stringReader.Dispose()
}

# Get the default Web Interface address
$webInterfaceAddress = "%adm-WebInterfaceUrl%"
if ([System.String]::IsNullOrEmpty($webInterfaceAddress))
{
    $Context.LogMessage("Default web interface address not set for Adaxes service. For details, see http://www.adaxes.com/help/?HowDoI.ManageService.RegisterWebInterface.html", "Warning")
}

# Add requestor information
$requestorGuid = [Guid]$Context.TargetObject.Requestor.Get("ObjectGuid")
$requestorDisplayName = $Context.GetDisplayNameFromAdsPath($Context.TargetObject.Requestor.AdsPath)
[void]$htmlBuilder.Append("<tr><td>Requestor:</td><td><a href='$webInterfaceAddress`ViewObject.aspx?guid=$requestorGuid'>$requestorDisplayName</a></td></tr>")

# Add approver information
$approverGuid = [Guid]$Context.TargetObject.ProcessedBy.Get("ObjectGuid")
$approverDisplayName = $Context.GetDisplayNameFromAdsPath($Context.TargetObject.ProcessedBy.AdsPath)
[void]$htmlBuilder.Append("<tr><td>Approved by:</td><td><a href='$webInterfaceAddress`ViewObject.aspx?guid=$approverGuid'>$approverDisplayName</a></td></tr>")

# Add target object information
$targetObjectGuid = [Guid]$Context.TargetObject.TargetObject.Get("ObjectGuid")
$targetObjectDisplayName = $Context.GetDisplayNameFromAdsPath($Context.TargetObject.TargetObject.AdsPath)
[void]$htmlBuilder.Append("<tr><td>Target object:</td><td><a href='$webInterfaceAddress`ViewObject.aspx?guid=$targetObjectGuid'>$targetObjectDisplayName</a></td></tr>")
[void]$htmlBuilder.Append("</table>")

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


Comments ( 0 )
No results found.
Leave a comment