Schedule a task to call a webpage using Task scheduler.

One frequent question I see in the newsgroups is "How do I schedule a task to run in IIS?”   This article discusses one technique using a VBS Script and Windows Task Scheduler.    Using Windows Task scheduler allows custom jobs to execute without having to stay logged into a server.    You can use this technique to request web pages frequently on a timed basis.  This keeps the page in-memory providing a better performance.   This could also request web pages to perform other administrative tasks. 

Here are steps to get started.

  • Write your VBS Script
  • Develop the webpage to process the HTTP Request
  • Create / Add table to database to track logging
  • Schedule the VBS Script

Write your VBS script

The script makes an HTTP request to a webpage on a timed basis.  (I.E. every 5 minutes).

Call LogEntry()

Sub LogEntry()

        ‘Force the script to finish on an error.
        On Error Resume Next

        'Declare variables
        Dim objRequest
        Dim URL

        Set objRequest = CreateObject("Microsoft.XMLHTTP")

       
'Put together the URL link appending the Variables.
        URL = "http://www.YourDomain.com/track.aspx

        'Open the HTTP request and pass the URL to the objRequest object
        objRequest.open "POST", URL , false

       
'Send the HTML Request
        objRequest.Send

        'Set the object to nothing
        Set objRequest = Nothing

End Sub
 
Track.aspx webpage

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="tracklog.aspx.vb" Inherits="Tracklog" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title>Log Page</title>
</head>

<body>

<form id="form1" runat="server">

<div></div>

</form>

</body>

</html>

Tracklog.aspx.vb code behind

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class Tracklog

    Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

‘Put your code in that would process when Track.aspx is requested
       Response.write(“Track.aspx was called:” & System.DateTime.Now())

       End Sub

End Class


How to schedule a task using Windows Task Scheduler

A scheduled task will allow a person to schedule any script, program, or document to run.   Scheduled Tasks run at the time specified which could be multiple times per minute, hour, or day.  The KB Article explains step-by-step.

·        http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&sd=tech

Refererence links

Published Thursday, September 28, 2006 11:09 PM by steve schofield
Filed under:

Comments

# re: Schedule a task to call a webpage using Task scheduler.

Monday, October 09, 2006 5:15 AM by EGS
Thanks. It's helpful. :)

# re: Schedule a task to call a webpage using Task scheduler.

Monday, October 09, 2006 9:02 PM by steve schofield

I'm glad this was useful.  

Steve Schofield

Microsoft MVP - IIS

# re: Schedule a task to call a webpage using Task scheduler.

Wednesday, March 14, 2007 7:31 PM by Rory Hunt

Really useful after loads of dodgy examples that didn't work. Thanks Steve.

# re: Schedule a task to call a webpage using Task scheduler.

Wednesday, April 04, 2007 7:09 AM by Riz

Great, it solved my problem to schedule a webpage to run every few minutes on local network. Thanks

# re: Schedule a task to call a webpage using Task scheduler.

Wednesday, April 04, 2007 10:25 AM by steve schofield

Glad this helped resolve your problem.

~Steve.

# re: Schedule a task to call a webpage using Task scheduler.

Monday, April 16, 2007 6:35 PM by Me

Can a vb script create a scheduled task?

# re: Schedule a task to call a webpage using Task scheduler.

Monday, April 16, 2007 9:00 PM by steve schofield

Yes, you would use ADSI or WMI to do this.  You could also use the 'RUN' method of VBS to execute the 'Scheduled Task' EXE.

http://www.google.com/search?hl=en&q=create+a+scheduled+task+with+vbscript

# re: Schedule a task to call a webpage using Task scheduler.

Tuesday, August 07, 2007 7:20 PM by Oz

what are the cons of this method ?

how long can the asp .neet script run ?

does the request being closed only after the script finished running ?

# re: Schedule a task to call a webpage using Task scheduler.

Wednesday, August 08, 2007 3:14 AM by steve schofield

The schedule task and VBS script don't really care how long the request is going to run or take to execute.  The script is solely used to 'request' or kick off your URL.

# re: Schedule a task to call a webpage using Task scheduler.

Wednesday, August 22, 2007 5:04 PM by John

What if the URL you're trying to call is in a password protected directory?  Is there a way to pass a user name and password?

# re: Schedule a task to call a webpage using Task scheduler.

Thursday, August 23, 2007 1:34 AM by steve schofield

Then I would do something like this article. www.4guysfromrolla.com/.../110100-1.2.shtml  Also check out the XMLHTTP Object model on msdn.microsoft.com.  This will give you all the properties and methods avaiable.

# re: Schedule a task to call a webpage using Task scheduler.

Saturday, August 25, 2007 9:35 PM by steve

it is very heplful. but i do not know how to run vbs script with windows scheduler. would you please drop a few lines? thanks

# re: Schedule a task to call a webpage using Task scheduler.

Monday, October 01, 2007 4:04 AM by MS Monir

Well I tried, but didn't get any response.... the ASPX page couldn't write into my MS Access database...

any answer????

# Application Pool warm-up.

Saturday, May 30, 2009 4:15 AM by ASPInsiders

I had a post the other day on forums.iis.net that I did some research and wanted to share. It was regarding

# Application Pool warm-up

Saturday, May 30, 2009 5:26 AM by Steve Schofield's Blog

I had a post the other day on forums.iis.net that I did some research and wanted to share. It was regarding