0 votes

I am working on a script that sends an email (using Send-MailMessage) to one or more recipients, selected through an AD Object Picker parameter within a custom command. I have researched this process on the Adaxes Q&A and have done my best to troubleshoot but I cannot seem to figure it out. See the code snippet below:

#Create recipients for email
$recipientList = $Context.GetParameterValue("%param-Recipients%")
$separator = ";"

$recipients = @()

foreach ($dn in $recipientList.Split($separator))
{
    $user = $Context.BindToObjectByDN($dn)
    $recipients += $user.Get("mail")
}

#Email settings
$To = $recipients

Every time I run the code, I get the error "cannot call a method on a null-value expression" referring to the $recipientList in the foreach line. I have of course chosen at least one user in the object picker before running the code so I am confused as to why this is not working.

by (120 points)

1 Answer

0 votes
by (288k points)
selected by
Best answer

Hello,

$recipientList = $Context.GetParameterValue("%param-Recipients%")

This line is incorrect. The thing is that %param-Recipients% resolves into the parameter value. At the same time the $Context.GetParameterValue method accepts the name of a parameter with the param- prefix, not the value of the parameter.

$recipients += $user.Get("mail")

Specifying an array of email addresses as the first parameter of the $Context.SendMail method will not work. If you want to directly pass all the recipients, they need to be in a single string separated by commas and spaces (e.g. "jdoe@company.com, jsmith@company.com"). As an alternative, you can directly send emails in the foreach block like in the below script.

$separator = ";" # TODO: modify me

# Email settings
$subject = "My subject" # TODO: modify me
$message = "My text" # TODO: modify me

# Get recipient DNs
$recipientDNs = $Context.GetParameterValue("param-recipients")

foreach ($dn in $recipientDNs.Split($separator))
{
    # Get user email
    $user = $Context.BindToObjectByDN($dn)
    try
    {
        $userEmail = $user.Get("mail")
    }
    catch
    {
        continue
    }

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

Related questions

0 votes
1 answer

I have a custom command that prompts for a "reporter" for our ticketing system for enabling VPN accounts. Sometimes a technician will enable an account on behalf of another ... do I extract the email address of that object chosen as a parameter? Thank you.

asked Dec 10, 2019 by bjzielinski (70 points)
0 votes
1 answer

Here is my issue, When I use this code: $DNs = %param-GroupsInRole% $Groups = $DNs -split "|" %Param-GroupsInRole% can have multiple groups. When setting up the parameter I am ... I just need to be able to do a foreach with the groups picked by the initiator.

asked Mar 23, 2023 by mightycabal (1.0k points)
0 votes
1 answer

I have tried it using the Custom Commands Action "Add the user to a group", which only allows me to add the user to one group at a time, and can't use the multiple DNs that the ... I can't get it to work. Could you assist me in finding the best way to do this?

asked Jan 16 by dominik.stawny (280 points)
0 votes
1 answer

I would like to change department without a script just yet if possible on multiple accounts. If I cant do this then I will entertain custom script Thanks :)

asked Nov 23, 2021 by will17 (350 points)
0 votes
1 answer

I am trying to find a way to get an hourly report on locked out user accounts to only be sent if the total amout of locked out account exceeds 10 users. Is this possible in ... a way to setup the logic to check to see how many items are returned in a report.

asked Jun 12 by Vertigo (50 points)
3,538 questions
3,229 answers
8,224 comments
547,747 users