Script Repository


Send notification after adding contact to distribution list

February 22, 2021
1392

The script sends a notification after adding a contact to a distribution list. The notification contains the list name, the contact name, e-mail address and also other distribution lists the contact is a member of.

To use the script with Adaxes, create a business rule triggered after adding a member to a group that runs the script.

Parameters:

  • $to - Specifies the email address of the notification recipient.
  • $subject - Specifies the email message subject.
  • $reportHeader - Specifies the email message header. In the header, the {0} placeholder will be replaced with the contact name, and the {1} placeholder will be replaced with the distribution list name.
  • $reportFooter - Specifies the email message footer.
You can use value references (e.g. %displayName%) when specifying the script parameters. When the script is run, they are replaced with corresponding properties of the distribution list.
Edit Remove
PowerShell
$to = "recipient@domain.com"
$subject = "Contact added to Distribution List '%displayName%'" # TODO: modify me
$reportHeader = "<h2><b>Contact {0} added to Distribution List {1}</b></h2><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

# 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")
}

# Bind to new member
$member = $Context.BindToObject("Adaxes://%member%")

if ($member.Class -ine "contact")
{
    return
}

# Get contact display name
$contactAdsPath = $member.AdsPath
$contactName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($member.AdsPath, 'IncludeParentPath')

# Get contact GUID
$contactGuid = $member.Get("objectGuid")

# Build link to contact
$contactLink = "<a href='$webInterfaceAddress`ViewObject.aspx?guid=$contactGuid'>$contactName</a>"

# Get contact mail
try
{
    $contactMailAddress = $member.Get("mail")
}
catch
{
    $contactMailAddress = "none"
}

# Get group name
$distributionListName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($Context.TargetObject.AdsPath, 'IncludeParentPath')

# Build link
$distributionListLink = "<a href='$webInterfaceAddress`ViewObject.aspx?guid=%objectGUID%'>$distributionListName</a>"

# Update report
$reportHeader = [System.String]::Format($reportHeader, $contactLink, $distributionListLink)
$reportHeader += "<p>Contact mail address: $contactMailAddress</p><p></p>"

# Check contact membership
# Build filter to find all Distribution Lists the contact is a member of
$filter = New-Object "System.Text.StringBuilder"
$filter.Append("(&(objectCategory=group)(!(groupType:1.2.840.113556.1.4.803:=2147483648))(mailNickname=*)(!(sAMAccountName=%sAMAccountName%))") | Out-Null
$filter.Append([Softerra.Adaxes.Ldap.FilterBuilder]::Create("member", $member.Get("distinguishedName"))) | Out-Null
$filter.Append(")") | Out-Null

# Find Distribution Lists
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$searcher = $Context.BindToObject("Adaxes://$domainName/rootDSE")
$searcher.SearchFilter = $filter.ToString()
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.SetPropertiesToLoad(@("objectGuid"))

try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $groups = $searchResultIterator.FetchAll()
    
    if ($groups.Length -eq 0)
    {
        $reportHeader += "<b>Contact is not a member of other Distribution Lists</b>"
    }
    else
    {
        $reportHeader += "<b>Other Distribution Lists the contact is a member of:</b><br/><ol>"
        foreach ($groupID in $groups)
        {
            # Get Distribution List GUID
            $guid = [Guid]$groupID.Properties["objectGuid"].Value
            
            # Get Distribution List name
            $name = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName("Adaxes://<GUID=$guid>", 'IncludeParentPath')
            
            # Update report
            $reportHeader += "<li><a href='$webInterfaceAddress`ViewObject.aspx?guid=$guid'>$name</a></li>"
        }
        $reportHeader += "</ol>"
    }
}
finally
{
    # Release resources
    $searchResultIterator.Dispose()
}

# Send mail
$report = $reportHeader + $reportFooter
$Context.SendMail($to, $subject, $NULL, $report)

Comments ( 0 )
No results found.
Leave a comment

Related Scripts