Automating Script Execution

[ LiB ]

Automating Script Execution

Depending on what you design your JScripts to do, you might want to run them at times when you are not working on your computer. For example, you might want to write a JScript that runs the Windows defrag.exe command-line utility. This command-line utility provides the capability to run the Windows Disk Defragmenter utility, which defrags files that are stored on hard disk drives .

The unfortunate thing about working the defrag.exe is that it is very resource intensive and makes working on your computer almost impossible while it is running. Therefore, most people generally start the defrag process at times when they know that they will not be using their computers. Another way to tackle this situation is to create a JScript that runs defrag.exe and then to schedule the execution of this script so that it runs during the middle of the night. For example, the following two-line JScript demonstrates how this script might look:

 Set WshShl = WScript.CreateObject("WScript.Shell") WshShl.Run "c:\Windows\System32\defrag C: /f" 

Windows provides two different ways to schedule the execution of your JScripts. Both of these options use the built-in Windows Scheduler service. The first option is to use the AT command. This command provides the capability to create, display, and delete scheduled tasks from the Windows command prompt. The second option available to you is to use the Windows Scheduled Task Wizard, which guides you through a series of steps that collect information from you that is required to set up a task that manages the scheduled execution of your script. Both of these scheduling options are discussed further in the sections that follow.

Scheduling Script Execution from the Windows Command Line

You can execute the Windows AT command from the Windows command prompt or from within a script. When it is executed from the Windows command prompt, you can view and administer scheduled tasks interactively. When it is executed from within a script, you can create a JScript that automates the creation and administration of scheduled tasks.

To view a list of currently scheduled tasks on your computer, open the Windows command prompt, type AT , and press the Enter key, as demonstrated here:

 C:\>at Status ID   Day            Time        Command Line -          1   Each M W F    11:30 PM     cmd /c TestScript.js          2   Each M W F    10:30 PM     cmd /c Cleanup.js          3   Each F        10:00 PM     cmd /c Archive.js C:\> 

As you can see, the output produced in this example shows three scheduled tasks, each of which is associated with a different JScript. The first script is called TextScript.js and has been set up to run as a scheduled task that executes at 11:30 P.M. every Monday, Wednesday, and Friday. The second script is called Cleanup.js and has been set up to run at 10:30 P.M. every Monday, Wednesday, and Friday. The third JScript is Archive.js and has been scheduled to execute at 10:00 P.M. every Friday. Also, take note that each scheduled task is assigned a unique status ID.

The following example demonstrates how to create a new scheduled tasks for a script called CopyTempFiles.js :

 AT 23:59 /EVERY:Su CMD /C CopyTempFiles.js 

The scheduled tasks set up by this statement will execute the script just before midnight every Sunday night. If this were the fourth scheduled task set up on the computer, it would be assigned a status ID of 4. By passing the number 4 to the AT command as an argument along with the /DELETE switch, as demonstrated below, you can delete the scheduled task.

 AT 4 /DELETE 

Alternatively, if you want to delete all currently configured scheduled tasks, you can run the AT command with the /DELETE switch without specifying any status ID, as shown here:

 AT /DELETE 

One benefit to using the AT command instead of the Windows Scheduled Task Wizard is that the AT command supports the capability to set up the scheduled tasks remotely on other network computers, as demonstrated here:

 AT \NtkFileServer 22:00 /EVERY:M,T,W,Th,F,S,Su CMD /C CopyTempFiles.js 

In this example, a scheduled task is set up to run a script called CopyTempFiles.js on a computer named NtkFileServer at 10:00 P.M., seven days a week.

NOTE

Note the use of the \\ characters in front of the network computer's name in the previous example.This is an example of using the Universal Naming Convention or UNC notation in which network resources are referenced in the format of \\ComputerName\ Path\SharedResource .

NOTE

To learn more about the AT command,open the Windows command prompt and type AT HELP .

To wrap up our discussion of the AT command, let's use the command to schedule the execution of the two-line Defrag.js script shown earlier in this section so that it will run automatically every Friday night, as shown here:

 at 23:30 /every:F cmd /c Defrag.js 

NOTE

When you run a JScript from the Windows command prompt or from the Windows desktop, the script executes under the context of your security privileges. In addition, it has access to your currently configured execution environment. In other words, if you have connections set up for network resources that the script needs in order to execute, its uses them. However, if you set up a JScript to run after hours, when you are not using your computer, it will not have access to your network drive connections.Therefore, you will have provide the script with the capability to establish its own connection, as demonstrated previously in this chapter.

Running the Windows Scheduled Task Wizard

If you prefer to work with wizards, you can use the Windows Scheduled Task Wizard to configure the automated execution of your JScripts. The wizard guides you through the setup process and will help you to set up any of the following automated execution schedules:

  • Daily

  • Weekly

  • Monthly

  • One Time Only

  • When My Computer Starts

  • When I Log On

By default, scheduled tasks run using the authority provided by a special built-in account known as LocalSystem . The problem with this account is that it has very limited security permissions. As a result, some of your scripts may fail to run. Fortunately, using the Windows Scheduled Task Wizard, you can configure scheduled tasks to run using a different user account, one with sufficient security privileges to get the job done.

NOTE

TIP

To keep things simple, you may want to create a special user account with sufficient security privileges to handle the automated execution of your JScripts. Also, you may want to set up this special user account so that its password never expires . Otherwise, your JScripts will stop executing the next time you change the account's password.

Setting Up a Scheduled Task

The following procedure outlines the steps involved in creating a new scheduled task using the Windows Scheduled Task Wizard.

  1. Click on Start and Control Panel. The Windows Control Panel appears.

  2. Click on Performance and Maintenance. The Performance and Maintenance dialog appears.

  3. Click on Scheduled Tasks. The Scheduled Tasks folder appears.

  4. Double-click on the Add Scheduled Task icon. The Scheduled Task Wizard begins to run. Click on Next to begin.

  5. A list of applications is displayed, as shown in Figure 7.16. Click on Browse to locate your JScript and then click on Open.

    Figure 7.16. Locate the JScript you want to set up to run as a scheduled task

    graphic/07fig16.gif


  6. Enter a descriptive name for the task and select a task schedule, as shown in Figure 7.17. Click on Next.

    Figure 7.17. Always give your scheduled tasks descriptive names .

    graphic/07fig17.gif


  7. If you selected the Daily option, you'll next see a dialog prompting you to select from the following list of options:

    • Start time. Sets the time the task is to be run.

    • Perform this task. Configures the task to run daily, on weekdays, or every __ days.

    • Start date. Sets the date on which the task should start running.

    Make your selections and click on Next, and then skip to step 13.

  8. If you selected the Weekly option, you'll next see a dialog prompting you to select from a list of options, as shown in Figure 7.18.

    Figure 7.18. Configuring your JScript to run on a weekly basis

    graphic/07fig18.gif


    • Start time. Sets the task's start time.

    • Every __ weeks. Configures the task to run every X number of weeks.

    • Select the day(s) of the week below. Sets one or more days of the week on which the task should run.

    Make your selections and then click on Next and then skip to step 13.

  9. If you selected the Monthly option, you'll next see a dialog prompting you to select from the following list of options:

    • Start time. Sets the time the task is to be run.

    • Day. Sets the date of the month on which the task is to run.

    • The __. Specifies the day of the month on which the task is to run.

    • Of the month(s). Sets the months in which the task is to run.

    Make your selections and click on Next, and then skip to step 13.

  10. If you selected the One Time Only option, you will be prompted to select from the following list of options:

    • Start time. Sets the task's start time.

    • Start date. Sets the task's start date.

    Make your selections and click on Next, and then skip to step 13.

  11. If you selected the When My Computer Starts option, you will be prompted to provide the following information, as shown in Figure 7.19.

    Figure 7.19. Setting up a scheduled task to run using a user account with sufficient security privileges

    graphic/07fig19.gif


    • Enter the user name. Sets the name of a user account whose security privileges should be used when running the task.

    • Enter the password. Sets the account's password.

    • Confirm password. Confirms the account's password.

    Make your selections and click on Next, and then skip to step 14.

  12. If you selected the When I Log On option, you will be prompted to provide the following information:

    • Enter the user name. Sets the name of a user account whose security privileges should be used when running the task.

    • Enter the password. Sets the account's password.

    • Confirm password. Confirms the account's password.

    Make your selections and click on Next, and then skip to step 14.

  13. The wizard then prompts you to provide the following information:

    • Enter the user name. Sets the name of a user account whose security privileges should be used when running the task.

    • Enter the password. Sets the account's password.

    • Confirm password. Confirms the account's password.

  14. Click on Finish.

Once created, the new scheduled task is stored in the Scheduled Tasks folder, as shown in Figure 7.20.

Figure 7.20. Viewing the scheduled task created by the Windows Scheduled Task Wizard

graphic/07fig20.gif


[ LiB ]


Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net