Script Repository


Email information on each approved request to create an object

May 19, 2016
1399

The script emails information on each request to create an AD object once it is approved, including 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.
Edit Remove
PowerShell
$subject = "New Object Created" # TODO: modify me
$to = "administrator@example.com" # 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 target object property
$targetObjectGuid = [Guid]$Context.TargetObject.Get("adm-TargetObjectGuid")
if ($targetObjectGuid -ne [Guid]::Empty)
{
    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)
$htmlBuilder.Append("<tr><td>Requestor:</td><td><a href='$webInterfaceAddress`ViewObject.aspx?guid=$requestorGuid'>$requestorDisplayName</a></td></tr>") | Out-Null

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

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

Comments ( 0 )
No results found.
Leave a comment