Lunarpages Web Hosting Forum

Author Topic: How to: Set up Plesk Scheduler to run a VBScript - with VBScript tips  (Read 10512 times)

Offline Comet Software

  • Spacescooter Operator
  • *****
  • Posts: 40
This article explains how to get a VBScript to run as a Plesk Scheduler task with Lunarpages as the web host.

First, we want to establish the environment in which we present this information.  This article was originally written and posted in May 2010 and these were the attributes of Lunarpages and our domain as it applies to this article:

  • Windows shared hosting plan
  • Plesk version 7.5.6
  • Microsoft Windows Script Host Version 5.6


Plesk Scheduler

“Plesk” is the domain administration software for Lunarpages Windows shared hosting.  “Scheduler” is the functionality for setting the times and parameters to automatically run scripts on the Lunarpages server within your domain.  It may be possible for Scheduler to run an executable, but, this article will only focus on running VBScripts.  A “task” is the collection of attributes related to launching and running an individual script.  These attributes include:
  • enabled
  • descriptive title
  • email address to send notification of the task having been run
  • path to the script engine to run
  • parameters to the script engine
  • task priority
  • time to launch the task

Enabled

This is a checkbox entitled “Enabled” to indicate whether or not to run the task.  Checking this “off” is helpful if your task is not running the way that you want and you wish to retain all of the task information while you research the problems.


Descriptive title

This is a textbox titled “Description” that helps you differentiate among your tasks.  If you do not fill it in when creating a task, it will default to the combination of the path to the script engine and the parameters to the script engine.


Notification email address

This is a dropdown combobox titled “Scheduler notification” for designating if you want Scheduler to send you an email when the task is run.  If you select the “Send to the email I specify” option, you type the email address in the associated textbox.


Path to the script engine

This is a textbox titled “Path to the executable file” and it contains the full path of the script engine executable name.


Parameters to the script engine

This is a textbox titled “Arguments” and it contains the full path of your script file, as well as any command line arguments (delimited by spaces) that you want to pass to your script.


Task priority

This is a dropdown combobox titled “Task priority” for designating how the Lunarpages server should prioritize your task in relationship to running other activities on the server at the same time.


Time to launch the task

The time attributes related to when to launch a task include:
  • hour(s) of the day
  • day(s) of the month
  • month(s) of the year
  • day(s) of the week


Creating a new task

The icon titled “Add New Task” launches the same screen with the attributes that you will be able to modify later.  Here is a sample of setting the attributes for a basic VBScript task:

   Enabled:            checked on
   Title:                  Our first test script
   Notification:       “Send to the email I specify” – myemail@somedomain.com
   Executable:       C:\Windows\system32\cscript.exe
   Arguments:       D:\Inetpub\vhosts\mydomain.com\httpdocs\myscript.vbs  arg_1
   Priority:             “Normal”
   Time:                “Every hour”   “Every day”    “Every month”    “Every day”


When the task is launched

The Lunarpages servers are set to the Pacific Time zone and your task will be launched at the “top of the hour(s)” that you have specified.  If you have designated that you want to receive an email notification, you will receive an email titled “The Plesk Scheduler notification” with an attachment.  The body of the email will look like this:

     User: KRAZ\mylunarpagesaccountname
     Running task: C:\Windows\system32\cscript.exe D:\Inetpub\vhosts\mydomain.com\httpdocs\myscript.vbs arg_1
     Started: Sat May 22 09:00:00 2010
     The task output is attached to the e-mail
     Ended successfully: Sat May 22 09:00:06 2010

The email attachment is a text file that shows the console output from running your script and looks like this:

     Microsoft (R) Windows Script Host Version 5.6
     Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

     Hello world!

If your path to the script engine is incorrect, your task will not launch and you will not receive the email notification.


Tip – notification email

We found that the option titled “Send to the default email” does not work in that we did not receive these emails to our default email address.  In the Help Tickets we sent to Lunarpages technical support in which we stated that we did not receive emails, we were never told that this option does not or might not work.

We recommend that you select the option titled “Send to the email I specify”.  The only times that we did not receive an email with this option was when we provided an incorrect path to the script engine.


Tip – output to the console

You may want to have your script send output to the console for debugging purposes or to show results from your script.  This is an example of code to do that:

    Wscript.echo “Hello world!”

If your script is launched successfully but generates an error, the output from the error will be displayed in the console.
 

Tip – environment variables

Your VBScript will be run in a console environment very similar to the “Command Prompt” on your local PC.  Lunarpages technical support told us that we did not have the proper rights to access the environment variables, but, we found that to be incorrect.  To list the environment variables that are available to you on the Lunarpages server, create and run a VBScript file with this code:

     Set WshShell = WScript.CreateObject("WScript.Shell")
     arrEnvironments = Array( "PROCESS", "SYSTEM", "USER", "VOLATILE" )
     For Each strEnv In arrEnvironments
          WScript.Echo strEnv & " environment variables:"
          Set WshSysEnv = WshShell.Environment(strEnv)
          For Each x In WshSysEnv
               WScript.Echo ""
               Wscript.Echo x
          Next
     Next

In your script, you will access the environment variables with code like this:

     Set oShell = CreateObject( "WScript.Shell" )
     user=oShell.ExpandEnvironmentStrings("%UserName%")
     comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
     WScript.Echo user & " " & comp


Tip – task attributes

After looking at the environment variables that were available to us within our script, we discovered that we could use them in the task attributes.  The “path” variable included the path to “cscript.exe” and the “pathext” variable included the “.exe” extension.  This allowed us to make this change to the task attribute:

     Executable:      cscript.exe

We also found that several variables provided full or partial path information that allowed us to make this change to the task attribute:

     Arguments:      %HOMEDRIVE%%HOMEPATH%\httpdocs\myscript.vbs  arg_1

Using these environment variables may be helpful to avoid typos and if Lunarpages decides to change drive/folder/directory names or structures at some point in the future.


Tip – accessing command line arguments

You can use command line arguments (a.k.a. parameters) to send information to the inside of your script.  This may be useful if you want your script to use a different date for processing or generate output to a different file name.  Command line arguments can be classified in three ways:

  • the order in which they appear on the command line
  • named
  • unnamed

We recommend using “named” command line arguments.  This allows you to have the arguments placed in any sequence and makes it easier to basically make all of the arguments optional.  Implementing named arguments will look like this:

     cscript   myscript.vbs   /name1:value1  /name2:value2  /name3:”Hello world!”

In your script, you will access the arguments with code like this:

     Set colNamedArguments = WScript.Arguments.Named
     varA = colNamedArguments.item("name1")
     if varA = "" then varA = “This is the default value for varA”
     varB = colNamedArguments.item("name2")
     if varB = "" then varB = “This is the default value for varB”


Tip – paths within your script

There may be times within your script that you will want to access other files within your domain.  You may want to read the contents of a text file or generate a text file that contains results from your script.  When you are testing/running your script in “Command Prompt” on your local PC, whatever folder/directory you are in when you launch your script is your “current directory”.  When running your script on the Lunarpages server, that may not be so straightforward.  To see what your current path is, create a VBScript file with this code:

     set x = CreateObject("Scripting.FileSystemObject")
     WScript.Echo "Current path =  " & x.GetAbsolutePathName(".")

If you run the script as a Scheduler task, your console output will be:

     Current path =  C:\Windows\system32

But, when you run this script from “Command Prompt” on your PC, it will display the path to the folder/directory from which you launched the command.

To help identify paths, we found some VBScript statements that may be more useful.  If you run a script with this code:

     WScript.Echo “Try #1 = “ & WScript.ScriptFullName
     WScript.Echo “Try #2 = “ & WScript.ScriptName
     WScript.Echo “Try #3 = “ & Replace(WScript.ScriptFullName, WScript.ScriptName, "")
     WScript.Echo “Try #4 = “ & Replace(WScript.ScriptFullName, "\"&WScript.ScriptName, "")

You will get console output that looks like this:

     Try #1 = D:\Inetpub\vhosts\mydomain.com\httpdocs\myscript.vbs
     Try #2 = myscript.vbs
     Try #3 = D:\Inetpub\vhosts\mydomain.com\httpdocs\
     Try #4 = D:\Inetpub\vhosts\mydomain.com\httpdocs


Tip – including a library file

As your development projects expand, you may find that you have procedures and functions that you reuse in multiple scripts.  The best way to utilize a library of text files that contain this code is to use the “ExecuteGlobal” statement.  This statement has a number of different ways to use it, but, for this article we will focus on using it to include a file that contains a function declaration.

First, create a text file (i.e. “myfunc.vbs”) in the same folder/directory as your main script with just the function declaration, such as code like this:

     Function get_gender_greeting ( gender )
          if gender = “male” then
               get_gender_greeting = “Hey dude!”
          end if
          if gender = “female” then
               get_gender_greeting = “Hello beautiful!”
          end if
     end function

Next, back in your main script, we need to open and read the text file containing your function declaration.  Then, we need to use the “ExecuteGlobal” statement to tell the script engine to import the contents of the text file into our main script.  We found that we might want to include multiple files, so, we created a procedure to execute this functionality.  We cut-and-paste this procedure at the top of all of our scripts and it looks like this:

     sub Include_File (File_to_include)
          dim obj_FileSystem, obj_File, str_File_Contents
          set obj_FileSystem = createObject ("Scripting.FileSystemObject")
          set obj_File = obj_FileSystem.openTextFile (File_to_include,1)
          str_File_Contents = obj_File.readAll ()
          executeGlobal str_File_Contents
     end sub

There are a number of ways that you can improve the above code to use constants, trap errors, display helpful information to the console, and, perform garbage collection.

To execute this procedure in your main script, create code that looks like this:

     my_path = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
     Include_File my_path&"myfunc.vbs"

The result of this is to effectively cut-and-paste the contents of “myfunc.vbs” into the main script at the location of the “Include_File” statement.  Then, you implement the “get_gender_greeting” function the same as if it had been declared within your main script:

     wscript.echo   get_gender_greeting(“male”)&”  Welcome to our world!”

The above code will result in output to the console that will look like this:

     Hey Dude!  Welcome to our world!


Closing

This article should get you started in using Plesk Scheduler to launch and run VBScript tasks on your Lunarpages web hosting server.  We have found that sometimes a script that we run on our local PC may not run the same when run as a Scheduler task.  In this article we tried to provide you with some tips that may help you debug the differences.

We hope that this article will be updated as this information changes in the future.  And, we encourage other Lunarpages web hosting customers to share what they have learned to help the next person that comes along with the same questions that you had.

Offline frankey

  • Newbie
  • *
  • Posts: 4
Re: How to: Set up Plesk Scheduler to run a VBScript - with VBScript tips
« Reply #1 on: August 15, 2010, 06:18:11 AM »
Great post dude this gives good knowledge keep it up dude

Thank you

« Last Edit: September 25, 2010, 10:33:35 PM by katrina1 »

MWaqas

  • Guest
Re: How to: Set up Plesk Scheduler to run a VBScript - with VBScript tips
« Reply #2 on: February 10, 2011, 05:07:59 AM »
Thankyou for the brief post, I would like to include one more thing if I may to schedule a VBScript file settings should be like this: Path to executable : C:\Windows\System32\cscript.exe or C:\Windows\System32\wscript.exe depending upon the language of your script. Hence if its C# or anyother just try cscript.exe and wscript.exe both.