Share via

Exchange Scripting Agent

Evgen Kotov 1 Reputation point
2023-05-02T11:29:06.38+00:00

Hello,

Help my in xml file for Scripting Agent

We have not been able to solve the problem for several days now.

where is the error here?

<?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"]
        $UsrAlias = (Get-Mailbox -Filter {SamAccountName -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()
    $UsrAlias = (Get-Mailbox -Identity $UsrIdentity).Alias.ToString()
    }
  
    chcp 65001
$smtpServer = "srvEXCH.contoso.com"
$from = "******@contoso.com"
$searchOU = "OU=etest,OU=Contoso org structure,OU=Contoso Organization,DC=contoso,DC=com"
$hourAgo = (Get-Date).AddHours(-1)
$employees = Get-ADUser -SearchBase $searchOU -Filter "whenCreated -ge '$hourAgo'" -Properties EmailAddress
$existingEmployees = Get-Content -Path "C:\employees.txt"
$subject = "Добро пожаловать в нашу компанию!"
$body = "<span style='font-family: Times New Roman; font-size: 16pt; font-weight: bold; font-style: italic;'>Уважаемый коллега!<br>
Добро пожаловать в АО  «Contoso»!<br>
Надеемся, что работа в нашей компании принесет Вам полезный опыт, новые впечатления и удовольствие от общения с коллективом!<br>
Для более быстрой адаптации рекомендуем пройти по нижеследующей ссылке и ознакомиться с Адаптационным курсом, разработанным специально для новых работников <a href='https://adaptaciya.contoso.com/'>https://adaptaciya.contoso.com/</a><br>
В случае возникновения вопросов по содержанию курса, просим обращаться к представителю ДУЧР, Имя Фамилия (<a href='mailto:******@contoso.com'>******@contoso.com</a>, 78-78-78)<br>
В случае возникновения вопросов технического характера (ссылка не открывается и др.), просим обращаться в Единый контакт-центр по номеру 9999 или на email <a href='mailto:******@contoso.com'>******@contoso.com</a>.<br>
Удачи Вам!
<br>

тел. 8787</span>"
Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -BodyAsHtml $body -Encoding utf8

foreach ($employee in $employees) {
    if ($existingEmployees -notcontains $employee.EmailAddress) {
        $to = $employee.EmailAddress
        Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -BodyAsHtml $body -Encoding utf8
        Add-Content -Path "C:\employees.txt" -Value $to
    }
}
 }
 # 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>
Exchange | Exchange Server | Management
Exchange | Exchange Server | Management

The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.


1 answer

Sort by: Most helpful
  1. Yuki Sun-MSFT 41,456 Reputation points Moderator
    2023-05-03T01:53:36.7933333+00:00

    Hi @Evgen Kotov ,

    From the xml file, seems like you are trying to send welcome Email to new users using Exchange Scripting Agent, right? If this is the case, instead of writing the scripts from scratch all by yourself, it's recommended to follow the steps in the blog below and make necessary changes into the sample scripts provided to fit your needs:
    Send Welcome Email to New Mailbox / New Users.

    Should it still doesn't work, could you clarify what exact problem you meet so that we can understand better about your situation? Is there any error message or it just doesn't work as expected?


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.