The cool thing about Windows task is that you can execute custom jobs to be executed in the server without the need of a user to be logged in.
The following code can be used to call ASPX web pages to run a specific job that needs to execute some sort of logic. The script is written in VB.NET, but the web page can be written with C# as the vbscript only is used to create the call from the server to the page.
The steps are as follow:
- Write the Script (VB in this case)
- Create an ASP.NET page
- Schedule the task through the VBS Script
1- Write the script
Call LogEntry()
Sub LogEntry()
On Error Resume Next
'variables
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
'URL link
URL = "http://www.YourDomain.com/MyWebPage.aspx
'Open the HTTP request and pass the URL
objRequest.open "POST", URL , false
'Send the HTML Request
objRequest.Send
'Set the object to nothing
Set objRequest = Nothing
End Sub
2- Create an ASP.NET page
<%@ 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>
Code behind (Here we are presenting an example that returns a page object with the message MyWebPage.aspx was called)
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class MyWebPageLog
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.write(“MyWebPage.aspx was called:” & System.DateTime.Now())
End Sub
End Class
3- Scedule a windows task:
NOTE : All the information below has been copied and pasted here from here http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&sd=tech
With Scheduled Tasks, you can schedule any script, program, or document to run at a time that is most convenient for you. Scheduled Tasks starts every time that you start Windows XP and runs in the background, and it starts each task that you schedule at the time that you specify when you create the task.Using Scheduled Tasks
To open Scheduled Tasks, click Start, click All Programs, point to Accessories, point to System Tools, and then click Scheduled Tasks.
To schedule a new task:
To schedule a new task:
- Double-click Add Scheduled Task to start the Scheduled Task Wizard, and then click Next in the first dialog box.
- The next dialog box displays a list of programs that are installed on your computer, either as part of the Windows XP operating system, or as a result of software installation.
Use one of the following procedures:- If the program that you want to run is listed, click the program, and then click Next.
- If you want to run a program, script, or document that is not listed, click Browse, click the folder and file that you want to schedule, and then click Open.
- Type a name for the task, and then choose one of the following options:
- Daily
- Weekly
- Monthly
- One time only
- When my computer starts (before a user logs on)
- When I log on (only after the current user logs on)
- Click Next, specify the information about the day and time to run the task, and then click Next.
Note that the information about the day and time to run the task vary depending on the selection that you made in the previous wizard dialog box. For example, if you chose Weekly, you must indicate the day of the week, the time, and if the task should run every week, every 2 weeks, every 3 weeks, and so on. - Type the name and password of the user who is associated with this task. Make sure that you choose a user with sufficient permissions to run the program. By default, the wizard selects the name of the user who is currently logged on.
- Click Next, and then click Finish after you verify the choices that you have made.
Using Advanced Options in Scheduled Tasks
If you want to change the configuration of the task, click Open in the advanced properties for the task before you click Finish. After you click Finish, the Properties dialog box opens for the task.
On the Schedule tab, you can change any of the scheduling options that you chose in the wizard, and you can also change the task configuration so that the task does not run too long, does not run if the computer is running on batteries (for laptops), and to specify whether or not the computer should be idle for the task to run.
NOTE: You can open the Properties dialog box for the task at any time if you open Scheduled Tasks, right-click the task, and then click Properties.
You cannot schedule a task so that it repeats in an interval less than one day; however, you can do this in the Properties dialog box:
On the Schedule tab, you can change any of the scheduling options that you chose in the wizard, and you can also change the task configuration so that the task does not run too long, does not run if the computer is running on batteries (for laptops), and to specify whether or not the computer should be idle for the task to run.
NOTE: You can open the Properties dialog box for the task at any time if you open Scheduled Tasks, right-click the task, and then click Properties.
You cannot schedule a task so that it repeats in an interval less than one day; however, you can do this in the Properties dialog box:
- Click the Schedule tab, and then click Advanced.
- Click to select the Repeat task check box, and then specify the number of minutes or hours in which you want the task to be repeated.
I hope you can find this useful.
No comments:
Post a Comment
Thank you for your thoughts. Your comment will appear in my blog shortly after review.
Have a great day!