Exchange ServerExchange Server 2016Exchange Server 2019Microsoft

Task Scheduler to Run PowerShell Script & Email Alerts

How to Automate Daily Report Alert with PowerShell Script In Exchange Server

Automate Task Scheduler to Run PowerShell Script for Email Alerts

If you want to set the task scheduler to run a PowerShell Script to generate daily reports, receive it as an email attachment.

You can do that easily with the help of Task Scheduler and a PowerShell script.

To email it as an attachment, you should have Exchange Server in your organization.

In this article, we will run the PowerShell Script to generate the Daily report for the user’s last logon.

Therefore, we will run the script at 1 am on a daily basis and will receive the report by email attachment.

By scheduling it on a daily basis, we will automatically receive an email every day regarding the user’s last logon in our email.

This automation will reduce the workload of an admin and help in managing users and computers easily.

 

Prepare a PowerShell Script to run in Task Scheduler

Our first step is to prepare our PowerShell script, which we want to automate.

In our case, we have Exchange 2019 installed, and we are going to schedule the task in Exchange Server itself.

We have the following script by the name of dailyReport.PS1 is saved inside the C drive.

$Date = Get-Date -Format "yyyy-MM-dd"
$ReportFolder = "C:\Reports"
if (!(Test-Path $ReportFolder)) {
    New-Item -ItemType Directory -Path $ReportFolder
}
$ReportFile = "$ReportFolder\DailyReport_$Date.csv"

# Export AD Users
$Users = Get-ADUser -Filter * -Property SamAccountName, DisplayName, Enabled, LastLogonDate
$Users | Export-Csv $ReportFile -NoTypeInformation -Encoding UTF8

# Send Email with Report Attachment
$sendMailMessageSplat = @{
    From        = 'Administrator <administrator@techijack.net>'
    To          = 'Jack <jack@techijack.net>'
    Subject     = 'Daily User Report'
    Body        = 'Please find the attached user daily report.'
    SmtpServer  = 'mail.techijack.net'
    Attachments = $ReportFile
}

Send-MailMessage @sendMailMessageSplat

script file for daily report

 

Set Task Scheduler to run the PowerShell Script

Open the Task Scheduler 

Once it is open, right-click on Task Scheduler and click on Create Basic Task

creating basic task in task scheduler

Now name the task, as in our case, we named it “Daily Report for Users Last Logon” and click on next.

Task Scheduler to run PowerShell script

After clicking on next, select the option Daily to start the task

selecting the task as daily

Once you proceed with Next, set the date and time to run the task and proceed with next.

In our case, we set the task to run at 1:00 AM

Task Scheduler to run PowerShell script & Email Alerts

Therefore, now you will get the screen to start the program.

Select the option “Start a Program” and proceed with the next step.

In the next screen, it will ask for the Program/script, add arguments, and start in.

Type PowerShell in Program Script

Therefore, in the Add argument type -NoProfile -ExecutionPolicy Bypass -File “C:\dailyreport.ps1”

And in Start in Type c:\

Setting Task Scheduler to run PowerShell script

Note: We have our “dailyreport.ps1” inside C: drive, same we have set in the argument as well, make sure you set the path and script file according to your configuration.

Now, click on Next and finish the basic task wizard.

So, we have successfully configured the task scheduler for our script to run at 1:00 AM

When the time reaches 1:00 AM, this script will run, and we will get the email in our jack user’s inbox.

 

Checking Result In Email

Script ran successfully, and we  received the email in the Jack user’s inbox

Now you can see, we have an email attachment by the name of Daily Report with the date.

Receiving an email alert for daily report

 

Conclusion

You can set the script for your desired report and send it as an attachment in the email.

Task scheduler will help to run the script on a daily basis, so that you can receive an email attachment every day.

You can add multiple scripts and get email alerts to reduce your everyday workload.

In case you have any issues with this article. Feel free to contact.

I hope you will also like some more tutorials on Exchange Server.

Moreover, if you want to see the article in action, watch the video below for Automating Task Scheduler to run a PowerShell script.

YouTube video

Vikas Jakhmola

Vikas Jakhmola, the founder of Techijack, with over 15+ years of experience in the IT industry.

Related Articles

Back to top button