Wednesday, May 22, 2019

Send an email on HTML format with the information of the latest New Mailbox created on your exchange server

The problem

Send an email with the latest mailbox created in your exchange server and email it to sombody

 

 

The Solution

Over a PowerShell script to

a. Open a Powershell for exchange or simple powershell with administrative privilegies (using Run as Administrator):

&nbsp

It can be done using this following commands

The correct way to run the script will be as follows
#name of the server (exchange server on-premises)
$smtpServer= "mail" 
#from address where you want to receive the email, can exist or not
$from = "abc@xyz.com"
#your email address (must exists, so you can actually receive the email
$to = "jose@domain.com"
$event =Get-EventLog -Log "MSExchange Management" | Where{$_.Message -Like "*Enable-Mailbox*"} | select -first 1 

if($event){
    $MailBodyString= $event | ConvertTo-Html 
    #WE CAN'T ATTACH THE FULL THML INTO THE FILE DIRECTLY SO WE NEED TO USE THE TEXT IN THE MIDDLE OF THE tables tags 
TAGS WHICH IS DONE USING REGEX
    $regex = new-object regex('(?s)(<table>(.*)<\/table>)', ([System.Text.RegularExpressions.RegexOptions]::Compiled -bor [System.Text.RegularExpressions.RegexOptions]::IgnoreCase))
    $MailBodyAsHtml = $Regex.Match($MailBodyString).VALUE
    Send-MailMessage -From $from -To $to -Subject "New Account created" -Body $MailBodyAsHtml -SmtpServer $smtpServer -BodyAsHtml
}
You will get an email in HTML format with the last event information And that's it! thank you for reading
Please consider to donate and thank you very much for reading this.

No comments:

Post a Comment