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 5Mike 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.

You may also like:  Methods to Enable and Disable Remote Desktop Locally

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.

You may also like:  Using Server Manager to uninstall the MiniShell or Full Graphical Shell to convert Windows Server 2012 R2 Datacenter to a Core Edition

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 74 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

Creating Users and Managing Passwords in Microsoft Office 365

0 168 1

In this Office 365 training video, instructor Spike Xavier demonstrates how to create users and manage passwords in Office 365.

Detailed Forensic Investigation of Malware Infections – April 21, 2015

2 271 3

In this IT Security training video, Security expert Mike Danseglio (CISSP / CEH) will perform several malware investigations including rootkits, botnets, viruses, and browser toolbars.

Write a Comment

See what people are saying...

  1. Chris

    How do you use this profile method but load Exchange PS with -NonInteractive so scheduled tasks don’t sit and wait for user interaction.

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

  3. Jared

    This script is great and definitely made me look good at work.

    Thanks for being our silent hero!

  4. Jeremy

    never mind i completely misread the profile at the type. tunnel vision (-) (-)

  5. Jeremy

    my goal is to script every night a refresh on public folder security – exchange 2007 is terrible for this-

    yet this only works ‘C:\program files\microsoft\exchange server\scripts>adduserstopfrecursive.ps1 -toppublicfolder “\%topfolder%” -user “%USER%” -permissions owner’. if i run this through the EMS it works great… i tried to setup a task in task scheduler and no go the task says it completed but the security is not changed – in addition you cannot run the above ps1 through regular powershell
    well i should say you can run it but nothing happen the cursor is just returned, same results as the task…there must be something more to the EMS…i am still researching on google any ideas would be great

Share your thoughts...

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