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

A Simple Introduction to Cisco CML2

0 3898 0

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

Creating Dynamic DNS in Network Environments

0 643 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader teaches how to create Dynamic DNS zones in Network Environments. Video Transcription: Now that we’ve installed DNS, we’ve created our DNS zones, the next step is now, how do we produce those … Continue reading Creating Dynamic DNS in Network Environments

Cable Testers and How to Use them in Network Environments

0 727 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments

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.