Script Repository


Notify about new member added to AD group and new member's manager

April 19, 2021
1875

The script notifies a recipient about a new member added to an AD group, including information on the manager of the new member in the notification email. To use the script with Adaxes, create a business rule triggered After adding a member to a group. To add the script to your rule, use the Run a program or PowerShell script action.

Parameters:

  • $to - Specifies the recipient(s) of the notification.
  • $subject - Specifies the email notification subject.
  • $messageTemplate - Specifies a template for the email notification message. In the template, the following placeholders will be replaced with the corresponding values:
    • {0} - display name of the new group member;
    • {1} - display name of the manager of the new member;
    • {2} - manager's email address.
    You can use value references (e.g. the %name%, %adm-ParentName%) in the message and subject. Value references will be replaced with corresponding property values of the group.
Edit Remove
PowerShell
$to = "recipient@domain.com" # TODO: modify me
$subject = "New Membership Notification" # TODO: modify me
$messageTemplate = @"
%adm-InitiatorFullName% (%adm-InitiatorUserName%) is adding {0} to group '%name%'.
You can contact the manager for further information:
Manager: {1}
Email address: {2}
"@ # TODO: modify me

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

# Get member manager
switch ($member.Class)
{
    "User"
    {
        try
        {
            $managerDN = $member.Get("manager")
        }
        catch
        {
            $managerDN = $NULL
        }
    }
    "Contact"
    {
        try
        {
            $managerDN = $member.Get("manager")
        }
        catch
        {
            $managerDN = $NULL
        }
    }
    "Group"
    {
        try
        {
            $managerDN = $member.Get("managedBy")
        }
        catch
        {
            $managerDN = $NULL
        }
    }
    default
    {
        $managerDN = $NULL
    }
}

if ($managerDN -eq $NULL)
{
    $managerName = "<manager not specified>"
    $managerEmail = "<none>"
}
else
{
    # Get manager Email
    $manager = $Context.BindToObjectByDN($managerDN)
    $managerName = $Context.GetDisplayNameFromAdsPath($manager.AdsPath)
    try
    {
        $managerEmail = $manager.Get("mail")
    }
    catch
    {
        $managerEmail = "<none>"
    }
}

# Build message
$memberName = $Context.GetDisplayNameFromAdsPath($member.AdsPath)
$message = [System.String]::Format($messageTemplate, @($memberName, $managerName, $managerEmail))

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

Comments ( 0 )
No results found.
Leave a comment

Related Scripts