Schedule Exchange Scripts - and email notification

 Hi, 

Some times you will need to schedule some task that should be done in non business hours. Instead of doing this manually, always I am trying to automate this tasks. You will gain experience and time for other things, like playing your favorite videogame.

In this example I'm going to migrate a mailbox account from my old Exchange Server to a new one, but you can fill the script with anything.

First, you will create the Exchange Powershell lines, and then you can put this lines inside a template scritp, which will send you a mail when the task start, and show the result of the commands. This will be useful if you need to know if the script is doing well or you need to throw away the joystick and connect to the server.

In this example we are using an Exchange 2016 Server.

I need to move the account juanito@example.com to the new database HOORAY-MX2

New-MoveRequest -Identity juanito@example.com -TargetDatabase HOORAY-MX2 -BadItemLimit 50 -Confirm:$false

#########################################
# PARAMETERS
#########################################
# Created by jmartinez at 15-12-2020
#Mail subject
$Asunto = "Migrating mailbox juanito to Exchange 2016"
#Mail body
$texto1='<html>
<body>
<div>
<p>Script execution Report</p>
<p><o:p>&nbsp;</o:p></p>
<p>You have executed the script '+$MyInvocation.InvocationName+' on the server '+$env:computername+'.</p>
<p>Execution details in attached file.</p>
<p>Automated Task</p>
<p>IT Department</p>
</div>
</body>
</html>'

$filedate=get-date -Format yyyy-MM-dd
$archivo="c:\tools\Reporte_Script_"+$filedate+".txt"


#########################################
# FUNCTIONS
#########################################
function LoadExchangeSnapin
{
    if (! (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn -ErrorAction:SilentlyContinue) )
    {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
    }
}
function enviaCorreo($Subject, $cuerpo, $adjunto)
{
$MailParams = @{
  SMTPServer = 'mail.example.com'
  Body = $cuerpo
  To = 'javier.martinez.c@outlook.com'
  Cc = 'support@example.com'
  From = 'jmartinez@example.com'
  Subject = $Subject
  BodyAsHtml = $true
  }
if($adjunto -ne $null){$archivo | Send-MailMessage @MailParams}
else{Send-MailMessage @MailParams}
}
#########################################
# MAIN
#########################################
#Start Transcript
Start-Transcript $archivo
#Load Exchange commands
LoadExchangeSnapin
#Migrate mailbox to new database
New-MoveRequest -Identity juanito@example.com -TargetDatabase HOORAY-MX2 -BadItemLimit 50 -Confirm:$false
sleep 30
Get-MoveRequest | Get-MoveRequestStatistics
#End transcript and send email
Stop-Transcript
enviaCorreo $Asunto $texto1 $archivo
########################################################################
# END
########################################################################


So, you should save this beauty as Auto_Migration.ps1, but now it's time to schedule.

Just create a task in the server. You can define the Triggers for the specific day and time to execute the script, but the funny part is the Action.



The Program/script should call:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments (optional):

-command "C:\tools\Auto_Migration.ps1"

Save the task and remember to use an account with Exchange and Admin privileges and set the task as "Run whether user is logged on or not".



Comments

Popular posts from this blog

Update DNS static servers in your local Network

Error 'General access denied error' (0x80070005). Starting SCVMM Resource

Powershell Scritp for Inventory VMS in Hyper-V and VMware