Running a Powershell script every 10 minutes (or so…)

So, you’ve written the next Powershell script that is going to save the world.  OK, so that might be stretching it a bit; but scripts can really make life easier by doing things like:
  • Scanning through AD to look for critical accounts that get locked and Unlock them - before they cause a problem
  • Email out reports derived from a SQL Query – that indicate you have lots of orders overdue in your on-line store
  • Determine the size of that ultra-important VIPs mailbox and e-mail their assistant to take a look before they get a warning (and ask you to fix it)

Most scheduled tasks are good to go once or twice a day.    Other tasks, like the ones above,  need to run every few minutes to keep the ship on an even keel.  The Task Scheduler in Windows has gotten much better since NT 4; but for some of the more advanced tasks, it can still be a bit tough to get working just right.

You can run a program every 5, 10, or 30 minutes using the Task Scheduler.  Here’s how:

  • Open the Task Scheduler in the Control Panel, and create a New Scheduled Task
  • You will need to click the Browse button and locate the executable for Powershell – it is normally found at :
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
  • Give it a descriptive name, and mark it to run Daily (don’t worry – we will be changing the schedule later). 
  • Next, you will be asked to enter the time for the script to start running. I like to choose 12:01 AM

Task Start Time

 

  • Now you will need to choose a username and password that will be used to run the script.  While it is ok to use your account for testing purposes, it is generally unwise to let the script run for long under your own personal account.  In addtion to the obvious security concerns with this, the script will fail to run if you forget to update the task after you’ve changed your password.  Indeed this will happen – when your password expires at 4:42pm on Friday afternoon as you are trying to get out of the door.   I guarantee you will forget and your important job will not run all weekend.  I also guarantee your boss will remind you with a nasty gram!  Some good guidelines to keep in mind when creating an account for your task:
    • Make the purpose of the account clear – Choose a descriptive name and document  what the the account is for (either in AD or on the local system)
    • Choose a good strong password – you won’t be typing this every day.  Make it strong
    • Make sure the account and it’s password does not expire – If either do, the script will not run – period
    • Understand the access the script needs and give it just that – If you give it too much, it can do damage (either by someone, or by errant code). 
  • At the summary screen, make sure you check the Open Advanced Properties box and then click Finish

Show Advanced Properties

  • This brings us to the heart of what we are trying to do – run your script – every few minutes.
  • In the Run text box, add a space after powershell.exe, and enter the full path to your script:

Script Path

  • Click on the Schedule tab, and then the Advanced button.  This will allow you to set all of the properties you need to make the script run as often as you need:

Advanced Properties Dialog

  •  
    • The End Date should be unchecked.  You want this to run all of the time.
    • Check the Repeat Task box, and set your interval
    • Setting the Until Time to 11:59PM will ensure the task repeats all day long and stops.  It will be started at 12:01AM the next day.
    • I usually set the checkbox to stop the task if it is still running.  You can determine if that is ok for your particular application.
  • When you have made all of the necessary adjustments, You can tell it to begin running for it’s first day by right-clicking on it and choosing Run Now.

 

One Last Tip

If your script will only be running on the local box, you might be able to have it run as the local SYSTEM account.    This should have access to most everything on the local computer.  To use this account:

  • Edit the properties for the  task after it has been created.
  • Enter “SYSTEM in the Run As text box, and click password.  Immediately click OK.

 Setting the task to run as the local system accoun

 

About these ads

Tags: ,

3 Responses to “Running a Powershell script every 10 minutes (or so…)”

  1. Jack Hughes Says:

    Oh my, that’s a very handy post… :) Thank you!

  2. Running a Powershell script every 10 minutes | TimeTag Says:

    [...] Cybersylum has created a great post outlining how to run a PowerShell script periodically. [...]

  3. sebastian Says:

    Hi!
    Thanks a lot for your great explanation!
    You help me a lot!
    Thanks !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: