How to schedule Exchange Server Scripts using PowerShell
How to schedule Exchange Server Scripts using PowerShell
I have this script that I wrote that generates a report for all the Sales people in the environment.
I’m going to reuse this script because I don’t want to manually run this to generate this report. What this does is actually take these sales users and dump the csv file out on the C drive. But I don’t want to have to manually do this. I want this to run once a week, once a month something like that.
What I want to do is to schedule this script to run with the Task Scheduler in the Exchange Management Console (EMC). On this machine, I’ve got my profile set‑up so I’ll open my Windows PowerShell profile.
Anytime PowerShell runs on this machine, it’s going to .source this RemoteExchange.ps1 script and connect to an Exchange server automatically.
If I were to execute PowerShell.exe from a scheduled task, this environment would get loaded for me and all I would have to do is call my ps1 script and it would just work. But, I do have to make sure that that environment is loaded either using this method or using PowerShell remoting.
From here I can schedule this ps1 script to run automatically, and that’s what we’re going to do here.
To start this off I’m going to go on the Control Panel, Administrative Tools and I’m going to go under the Task Scheduler.
When this comes up what I’m going to do is go into action and I’m just going to do a Basic Task.
I’ll call it “Sales Report” and hit next.
I guess I’ll do this weekly for now and we’ll run this Monday through Friday, and we’ll do it 12am.
What I want to do for the action is start a program.
And that program is going to be PowerShell.exe. If you look at the Exchange Management Shell shortcut, they’re already doing this to begin with. You look at the target of the shortcut, you can see that they’re calling powereshell.exe and passing the other ps1 script.
I’m going to steal this from here.
That’s going to be my program.
My arguments would be dash command and the path to my script.
In this case my script file here is located in my documents but to make this easier to type I’m going to dump this in the C drive as well.
So C:\\SalesMBReport.ps1 and hit next.
Now that’s pretty much it for defining the schedule task. But what I want to do is open this thing when it’s done and go into the properties and change a couple of things. I’ll go ahead and check this option and click finish.
In the Properties, I’m going to do a couple of things. First of all, I want this to run whether the user is logged on or not.
So that’s important. If somebody kicks me off this machine at night, my report might not run if this isn’t selected.
Make sure that’s set. I’m also going to select the option to run with highest privileges. If I have UAC enabled or something like that, that would mean it would run PowerShell.exe elevated. Or if it is trying to access anything or dump a report in a directory where you normally would need administrator permissions, this will take care of that. I’m going to go ahead and say OK to this, type in my credentials.
In this case, really, what we’re saying here is we’re scheduling the script to run under the administrator account. We’re going to load the administrator profile when this kicks off. That will load off the Exchange environment and everything should work out just fine.
Now I’m here under my task schedule library. I’ve got this sales report that I’ll run every day at 12am during the week, and right now just to test it I’m going to right click and do a run.
If we look at the script one more time, its job is to go out and pull this information, dump it on a file in C drive.
A file doesn’t currently exist so let’s go ahead and run this task manually and see if it works. Right click and go to run, and if I do a view or if I come up here to the actions pane and do a refresh. Status is now running, so it’s actually doing its thing.
Hit refresh again. Still running so it’s going to take a second to load those commands. Get everything working.
But let me go into the C drive and take a look. As soon as the task is done we should have the csv file here.
OK so the csv file has automatically shown up on the root of the C drive. Come back over here and do a refresh. We can see that we’re now ready so I went to the process of running the task.
It’s succeeded. Put the file out there and there’s my data.
As you can see it’s pretty straightforward, and basically just create your ps script and run through the Wizard, do a basic task and schedule that to run on a weekly basis or daily basis or whatever works for you.
Just make sure that either through a profile or at the beginning of your script, you’re automatically loading the Exchange environment either using Exchange tools or through remoting, to make sure that your commands will successfully run.
You May Also Like
Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2
In this video, Security Instructor Mike Danseglio demonstrates how to use BitLocker in Window 10 to secure files on a USB Flash drive that adhere to stricter data protection requirements as found inside Government entities. BitLocker 2-day instructor-led training is now available at Interface: BITLOCK: Planning and Deploying BitLocker Drive Encryption Training Video Transcription: Hi. … Continue reading Government Edition – Encrypting a USB Flash Drive in Windows 10
Hi, my name is Mike Danseglio. I’m an instructor here at Interface Technical Training. I want to talk a little bit about encrypting USB flash drives with Windows 10. The concept of protecting data when it’s on a USB flash drive is not a new concept. BitLocker 2-day instructor-led training is now available at Interface: … Continue reading Encrypting a USB Flash Drive in Windows 10