How to schedule Exchange Server Scripts using PowerShell

Home > Blogs > Exchange Server > How to schedule Exchange Server Scripts using PowerShell

How to schedule Exchange Server Scripts using PowerShell

Like This Blog 5 Mike Pfeiffer
Added by September 10, 2013

I have this script that I wrote that generates a report for all the Sales people in the environment.

001-sheduling-exchange-server-scripts-in-powershell

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.

002-profile-sheduling-exchange-server-scripts-in-powershell

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.

003-sheduling-exchange-server-scripts-in-powershell

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.

004-task-scheduler-exchange-server-scripts-in-powershell

When this comes up what I’m going to do is go into action and I’m just going to do a Basic Task.

005-basic-task-scheduler-exchange-server-scripts-in-powershell

I’ll call it “Sales Report” and hit next.

006-basic-task-scheduler-exchange-server-scripts-in-powershell

I guess I’ll do this weekly for now and we’ll run this Monday through Friday, and we’ll do it 12am.

007-basic-task-scheduler-exchange-server-scripts-in-powershell

What I want to do for the action is start a program.

008-start-a-program-task-scheduler-exchange-server-scripts-in-powershell

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.

009-start-a-program-task-scheduler-exchange-server-scripts-in-powershell

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.

010-start-a-program-task-scheduler-exchange-server-scripts-in-powershell

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.

011-start-a-program-task-scheduler-exchange-server-scripts-in-powershell

So C:\\SalesMBReport.ps1 and hit next.

012-start-a-program-task-scheduler-exchange-server-scripts-in-powershell

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.

013-start-a-program-task-scheduler-exchange-server-scripts-in-powershell

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.

014-properties-scheduler-exchange-server-scripts-in-powershell

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.

015-run-as-administrator-properties-scheduler-exchange-server-scripts-in-powershell

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.

016-run-script-scheduler-exchange-server-scripts-in-powershell

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.

017-run-script-scheduler-exchange-server-scripts-in-powershell

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.

018-run-script-scheduler-exchange-server-scripts-in-powershell

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.

019-run-script-csv-scheduler-exchange-server-scripts-in-powershell

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.

020-run-script-csv-scheduler-exchange-server-scripts-in-powershell

It’s succeeded. Put the file out there and there’s my data.

021-data-run-script-csv-scheduler-exchange-server-scripts-in-powershell

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.

Videos You May Like

Agile Methodology in Project Management

0 156 0

In this video, you will gain an understanding of Agile and Scrum Master Certification terminologies and concepts to help you make better decisions in your Project Management capabilities. Whether you’re a developer looking to obtain an Agile or Scrum Master Certification, or you’re a Project Manager/Product Owner who is attempting to get your product or … Continue reading Agile Methodology in Project Management

Subnetting a TCP/IP Network using the Magic Box Method

0 1736 5

See our class schedule for complete Course Schedule Training. Classes are held in Phoenix, AZ and can be attended online from anywhere in the world with RemoteLive™. Instructor: Rick Trader  Video Transcription: One of the things that we might have to do in our corporate network is to take a class of IP addresses and then subnet that into … Continue reading Subnetting a TCP/IP Network using the Magic Box Method

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 630 5

How does an investigator hunt down and identify unknown malware? In this recording of our IT Security training webinar on April 21, 2015, Security expert Mike Danseglio (CISSP / CEH) performed several malware investigations on infected computers and identify symptoms, find root cause, and follow the leads to determine what’s happening. He demonstrated his preferred … Continue reading Detailed Forensic Investigation of Malware Infections – April 21, 2015

Write a Comment

See what people are saying...

  1. Pingback: Retaining Mailbox for Disbaled User Accounts | Sultan Rayes | سلطان ريس

Share your thoughts...

Please fill out the comment form below to post a reply.