Send Welcome Email to New User using the Scripting Agent

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


  • Edited by Guji Sunday, March 01, 2015 10:30 AM Formatting
March 1st, 2015 10:25am

I guess its a replication issue. can you try to increase sleep time and check ?

Regards

John

Free Windows Admin Tool Kit Click here and download it now
March 1st, 2015 12:25pm

John, Thanks for the reply. I have tried increasing the sleep time to 60 seconds but this still does not work when creating the User Account and Mailbox using the Exchange 2013 ECP.  Like previously mentioned when invoking the Enable-Mailbox for a user account already created everything works fine.

March 1st, 2015 2:27pm

Hi

Issue 2 has been resolved after much perseverance today. I have changed certain lines of code

From:

$USRdfirst=$provisioningHandler.UserSpecifiedParameters["FirstName"]
$USRdlast=$provisioningHandler.UserSpecifiedParameters["LastName"]
$UsrAlias=(Get-Mailbox -Filter {SamAccountName -eq $UsrSamAccountName}).Alias.ToString()
$USRdname=$USRdfirst + " " + $USRdlast

To:

$Usrname = (Get-Mailbox -identity $UsrIdentity | Select Name | foreach { $_.Name})
$UsrString = $Usrname | Out-String

Issue one however is still not resolved.

Free Windows Admin Tool Kit Click here and download it now
March 1st, 2015 4:20pm

Ok I am gonna to answer my own question.

I have persevered through the day and have resolved my two issues.  I hope this assist others within the community.

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.
	Start-Sleep -s 30
    Set-ADServerSettings -ViewEntireForest $true
    # New-Mailbox triggered. Taking SamAccountName parameter.
    if ($provisioningHandler.UserSpecifiedParameters.Contains("Name") -eq $true) {
	$UsrSamAccountName = $provisioningHandler.UserSpecifiedParameters["Name"]
    	$Usrname = (Get-Mailbox -identity $UsrSamAccountName | Select Name | foreach { $_.Name})
    	$UsrString = $Usrname | Out-String
	$UsrAlias = (Get-Mailbox -Filter {Name -eq $UsrSamAccountName}).Alias.ToString()
    	}
    # 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()
    	$Usrname = (Get-Mailbox -identity $UsrIdentity | Select Name | foreach { $_.Name})
    	$UsrString = $Usrname | Out-String
    	$UsrAlias = (Get-Mailbox -Identity $UsrIdentity).Alias.ToString()
	}
  
    # 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",$UsrString 
        $fileHR = "c:\welcome\WelcomeMSG.htm"
        $smtp="x.x.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>


  • Edited by Guji Sunday, March 01, 2015 6:22 PM
  • Marked as answer by Guji Sunday, March 01, 2015 6:22 PM
March 1st, 2015 6:22pm

I have a working PowerShell script that sends automated mail when a new user mailbox is created or enabled on exchange server 2013, this works fine except that the images and logos in the html file does not display.

I have read somewhere that you have to add the images as a linked resource.

Anyone know the solution to this?

Thanks. 

Free Windows Admin Tool Kit Click here and download it now
June 9th, 2015 6:36am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics