0 votes

I found these codes: http://www.adaxes.com/sdk/SampleScripts ... lFlow.html
I created custom command(action is "run powershell script") with the following codes and added it to a scheduled task , then ran
I want to get users' (in a specific OU)mailbox forwarding address, or display name ,email address.. so i only use "forward to" part .

# Get mailbox parameters
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to user object
$userDN = "CN=John Smith,CN=Users,DC=domain,DC=com" [b] (change to a user exists in our AD)[/b]
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

# Get Exchange properties
$mailboxParams = $user.GetMailParameters()

# The $mailboxParams variable contains properties of an Exchange mailbox

# Get Delivery Options
$deliveryOptions = $mailboxParams.MailFlowSettings.DeliveryOptions

# Forward to
$forwardTo = $deliveryOptions.ForwardingAddress.DisplayName
Write-host "Mail forwarding: " -NoNewline
if ($forwardTo -ne $NULL)
{
    Write-Host "Enabled"
    Write-host "`tForward to: $forwardTo"

    # Deliver message to both forwarding address and mailbox
    Write-Host "`tDeliver message to both forwarding address and mailbox:" `
        $deliveryOptions.DeliverToMailboxAndForward
}
else
{
    Write-Host "Disabled"
}

But , nothing output in the result pane. I got this error : Cannot invoke this function because the current host does not implement it.

Can anyone help me look into this ? Really appreciate!

by (50 points)

1 Answer

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

Hello Tony,

The thing is that the script is supposed to be run directly from the PowerShell Console, and not from Business Rules, Custom Commands or Scheduled Tasks. It uses the Write-Host cmdlet that is not supported in scripts run by Adaxes service.

If you want to use the script with the Run a program or PowerShell script action, you can use the following version:

# Get mailbox parameters
$mailboxParams = $Context.TargetObject.GetMailParameters()
# The $mailboxParams variable contains properties of the user mailbox on which the script is run

# Get Delivery Options
$deliveryOptions = $mailboxParams.MailFlowSettings.DeliveryOptions

# Forward to
$forwardTo = $deliveryOptions.ForwardingAddress.DisplayName

if ($forwardTo -ne $NULL)
{
    $Context.LogMessage("Mail forwarding enabled. Forward to: $forwardTo", "Information")
    $Context.LogMessage("Deliver message to both forwarding address and mailbox: " + $deliveryOptions.DeliverToMailboxAndForward, "Information")
}
else
{
    $Context.LogMessage("Mail forwarding disabled.", "Information")
}
0

Thanks for your reply !

I'm a noob of using adaxes and feel a little confused about those commands. Your reply is really helpful.

I'm going to talk more about my case.

I created a custom command, actions:

a. If the User is located under the 'testOU' container AND
b. If account is inactive for more than 26 weeks AND
c. If the User has an Exchange mailbox then (here , i can get %distinguishname% or %fullname% something else)
d. Run PowerShell script 'get forwarding address' for the User (i can use the script that you provided, thanks)
e. Send e-mail notification to "$forwardTo"

Part d,
I think I can use(but I don't know how can I get the value from part a/b/c and put it into part d, and eventually get the value $forwardTo ) :

# Get mailbox parameters
$mailboxParams = $Context.TargetObject.GetMailParameters()
# The $mailboxParams variable contains properties of the user mailbox on which the script is run

# Get Delivery Options
$deliveryOptions = $mailboxParams.MailFlowSettings.DeliveryOptions

# Forward to
$forwardTo = $deliveryOptions.ForwardingAddress

Thank you so much !

0

Hello Tony,

Actually, you can combine d and e together and send e-mail notifications with the help of a PowerShell script. The following script sends a notification specified by $mailTemplate to the user to whom mail is forwarded. The notification email subject is specified by $mailSubject.

$mailSubject = "%name%'s mail in your inbox" # TODO: modify me

$mailTemplate = "
Hello,

This is to notify you that you'll be receiving %name%'s mail for your reference.

Do not reply to this e-mail, it has been sent to you for notification purposes only.
" # TODO: modify me

# Get mailbox parameters
$mailboxParams = $Context.TargetObject.GetMailParameters()
# The $mailboxParams variable contains properties of the user mailbox on which the script is run

# Get Delivery Options
$deliveryOptions = $mailboxParams.MailFlowSettings.DeliveryOptions

# Forward to
$forwardTo = $deliveryOptions.ForwardingAddress.ObjectGuid

if ($forwardTo -ne $NULL)
{
    # Get the address mail is forwarded to
    $altRecipient = $Context.BindToObject("Adaxes://<GUID=$forwardTo>")
    try
    {
        $forwardingAddress = $altRecipient.Get("mail")
    }
    catch
    {
        $Context.LogMessage("Cannot notify the recipient of %name%'s forwarded mail because the recipient doesn't have an e-mail address.", "Error")
        return
    }

    $Context.SendMail($forwardingAddress, $mailSubject, $mailTemplate, $NULL)
}
else
{
    $Context.LogMessage("Mail forwarding disabled for %name%.", "Warning")
}

For more information on sending e-mails in PowerShell scripts executed by Business Rules, Custom Commands and Scheduled Tasks, see Sending Emails and SMS.

Related questions

0 votes
1 answer

Hey guys, First time Adaxes user, and let me say, we absolutely love the product! Quick question though.... As a part of our account de-provisioning process, we ... exact same code in a regular powershell window with no problem. Any thoughts? Any alternatives?

asked Sep 6, 2012 by sco.robinso (20 points)
0 votes
1 answer

Hi there ! I wan to implement a custom command that will remove a user from all disitribution groups : Import-Module Adaxes Get-AdmGroup -Filter { mail -like "*" } | Where { ( ... host does not implement it. I can't see what i'm doing wrong... Thanks Stephen

asked Sep 27, 2011 by sroux (800 points)
0 votes
1 answer

I wrote a small script to get Mail Queue stats on all my Exch 2010 Hub Tansport servers. It works in EMS but when I configure as a Custom Command it returns an ... $HubServer) { get-Queue -server $server -EA inquire -SortOrder -messagecount write-output "" }

asked Dec 11, 2012 by mdeflice (350 points)
0 votes
1 answer

Our SQL DBAs are working to move most/all our SQL connections to SSL. They have asked if Adaxes can support this.

asked Feb 22, 2021 by ggallaway (300 points)
0 votes
1 answer

I don't understand how you would use this searcher function. Can you show me in this example? Import-Module ImportExcel #set up variables $currentTime = Get-Date ... $Context.BindToObjectByDN($NewU) $U.Put("adm-CustomAttributeBoolean6", $False) $U.SetInfo() }

asked Dec 14, 2023 by mightycabal (1.0k points)
3,548 questions
3,239 answers
8,232 comments
547,814 users