Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

Send Welcome Email to New User using the Scripting Agent

$
0
0

Hi

I require some assistance please.  I have found a number of scripts online to send a Welcome Email to new users whereby they make use of the ScriptingAgent.xml file.

I like the way the scripts have been configured and have mashed up one or two scripts together for my desired result however when testing in my lab (Exchange 2013) I have found tow problems.

The problems are:

1. When creating a new user and mailbox (New-Mailbox) via the ECP the script throws out an error which I will post later in this post. The mailbox is created fine but it does not send out a welcome email.  When a user is created in ADUC and then a mailbox is created (Enable-Mailbox) the welcome email is sent out correctly.

2. In the script i have a section to check for NewUser00 in the HTML file and replace that with the Users Name and Surname, however this does not work correctly, I just come up blank:

The Script:

<?xml version="1.0" encoding="utf-8" ?><Configuration version="1.0"><Feature Name="WelcomeEmail" Cmdlets="New-Mailbox,Enable-Mailbox"><ApiCall Name="OnComplete">

if($succeeded) {
    # Waiting for synchronization after mailbox has been created.
    Set-ADServerSettings -ViewEntireForest $true
    Start-Sleep -s 10
    # New-Mailbox triggered. Taking SamAccountName parameter.
        if ($provisioningHandler.UserSpecifiedParameters.Contains("SamAccountName") -eq $true) {
	$UsrSamAccountName = $provisioningHandler.UserSpecifiedParameters["SamAccountName"]
	$USRdfirst = $provisioningHandler.UserSpecifiedParameters["FirstName"]
	$USRdlast = $provisioningHandler.UserSpecifiedParameters["LastName"]
	$UsrAlias = (Get-Mailbox -Filter {SamAccountName -eq $UsrSamAccountName}).Alias.ToString()
	$USRdname = $USRdfirst + " " + $USRdlast
        }
    # Enable-Mailbox triggered. Taking Identity parameter, this is the only one avalaible in this case.
    if ($provisioningHandler.UserSpecifiedParameters.Contains("Identity") -eq $true) {
    $UsrIdentity = $provisioningHandler.UserSpecifiedParameters["Identity"].ToString()
    $USRdfirst=$provisioningHandler.UserSpecifiedParameters["FirstName"]
    $USRdlast=$provisioningHandler.UserSpecifiedParameters["LastName"]
    $UsrAlias = (Get-Mailbox -Identity $UsrIdentity).Alias.ToString()
    $USRdname= $USRdfirst + " " + $USRdlast
    }

    # Defining variables.
        $UsrAddr = (Get-Mailbox -Filter {Alias -eq $UsrAlias}).PrimarySmtpAddress.ToString()
        $UsrOU = (Get-Mailbox -Filter {Alias -eq $UsrAlias}).OrganizationalUnit

            # Sending email notification to the user in specific OU.
        if ($UsrOU -match "Contoso.com") {

        ####################
        #      HR          #
        ####################
        $fromHR="HR@contoso.com"
        $SubjectHR="'Welcome to Contoso'"
        $BodyHR = [string] (get-content ("c:\welcome\WelcomeMSG.htm"))
	$BodyHR = $BodyHR -replace "NewUser00",$USRdname
        $fileHR = "c:\welcome\WelcomeMSG.htm"
        $smtp="192.168.x.x"
        Send-MailMessage -From $fromHR -To $UsrAddr -Subject  $SubjectHR -Body $BodyHR -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -SmtpServer $Smtp
        ####################
 }
 # Clearing variables. Each one in its own line in order to prevent error messages from being shown on EMC.
 if ($UsrAlias) { Remove-Variable UsrAlias }
 if ($UsrAddr) { Remove-Variable UsrAddr }
 if ($UsrOU) { Remove-Variable UsrOU }
 if ($UsrMsg) { Remove-Variable UsrMsg }
 if ($UsrIdentity) { Remove-Variable UsrIdentity }
 if ($UsrSamAccountName) { Remove-Variable UsrSamAccountName }
}</ApiCall></Feature></Configuration>

The Error for issue 1:

The cmdlet extension agent with the index 5 has thrown an exception in OnComplete().
The exception is: Microsoft.Exchange.Provisioning.ProvisioningException: ScriptingAgent:
Exception thrown while invoking scriptlet for OnComplete API: You cannot call a method on a
null-valued expression.. ---> System.Management.Automation.RuntimeException:
You cannot call a method on a null-valued expression. at CallSite.Target(Closure , CallSite , Object ) at
System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at
System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame) at
System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
 --- End of inner exception stack trace --- at Microsoft.Exchange.ProvisioningAgent.ScriptingAgentHandler.OnComplete(Boolean succeeded, Exception e)
 at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnCompleteImpl(Task task, Boolean succeeded, Exception exception)


Any assistance in resolving these two issues will be really appreciated



Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>