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 (226k 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

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 (330 points)
0 votes
1 answer

We're trying to setup a new action in our Web interface that runs on User's that are currently in an OU called 'New Starters' that is in all of our domains (An ... no results, There are no other fields we can use that are unique to accounts in those OUs

asked Feb 7, 2020 by richarddewis (240 points)
0 votes
1 answer

Hello The search function in the AD object picker seems to be limited to the attribute "Name". I have some AD groups where the name is not very meaningful for our ... these cases. Is there any way to include the "Description" attribute in the search? Pudong

asked Sep 26, 2021 by pudong (560 points)
0 votes
1 answer

Hi, I am Create a Custom Command. The Parameters UI Control is AD object picker. The Action is add the Parameters in Extension Attribute 1 But it will add the AD DN. If I want to add LogonName, what parameters should I enter?

asked Aug 2, 2021 by will.chc.join (90 points)
2,779 questions
2,512 answers
6,574 comments
26,126 users