We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Send mail message to group members

February 24, 2021 Views: 2416

The script can be executed on an AD group to send an email message to all its members. You can use the script in business rules, custom commands and scheduled tasks executed on Group objects.

Parameters:

  • $subject - Specifies the message subject.
  • $messageText - Specifies the message body text.
    You can use value references in the message subject and text. They will be replaced with the properties of the group on which the script is executed. For example, if you insert %name% in the message text, it will be replaced with the name of the group on which the script is run.
Edit Remove
PowerShell
$subject = "Notification to %name% members" # TODO: modify me
$messageText = @"
Attention to all %name% members!

This is a notification from your support team.
"@ # TODO: modify me

try
{
    $memberGuidsInByte = $Context.TargetObject.GetEx("adm-MembersGuid")
}
catch
{
    return # The group has no members
}

$toAddresses = @()
foreach($memberGuidInByte in $memberGuidsInByte)
{
    $memberGuid = [guid]$memberGuidInByte
    $groupMember = $Context.BindToObject("Adaxes://<GUID=$memberGuid>")

    if($groupMember.Class -ieq "group")
    {
        continue # Skip groups because we will send emails to their members anyway
    }
    
    try
    {
        $mail = $groupMember.Get("mail")
    }
    catch
    {
        continue # The member doesn't have an email address
    }
    
    $toAddresses += "$mail"
}

if ($toAddresses.Length -gt 0)
{
    $toAddressesString = [System.String]::Join(",", $toAddresses)
    $Context.SendMail($toAddressesString, $subject, $messageText, $NULL)
}
To send the message only to direct group members, replace adm-MembersGuid with adm-DirectMembersGuid.
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers