Verify Network Availability And Recover Example

In this example, we will create a task that pings a remote server every hour, and verifies that a ping response is received.  If not, an entry is written to a text file and an attempt is made to reboot the server.  This example will only work on Windows NT, 2000 or XP.

WHAT'S COVERED:
* Creating a Managed Task without using the Add Task Wizard
* Using the Schedule Watcher Trigger to trigger a task every hour
* Pinging a remote machine
* Writing information to a file using the File Write action
* Using AutoMate variables
* Using expressions
* Rebooting remote machines
 

NOTE: This example assumes that the Add Task Wizard is off .  This is not default behavior, but speeds task creation for advanced users.  To ensure the Add Task Wizard is off for this example, go to the System | Options menu in the AutoMate Task Administrator, and from the General tab, make sure "Use Add Task Wizard when adding tasks" is unselected.

Step 1:
Click the New button from the AutoMate Task Administrator.  A new task will appear in the Managed Task view.  Select the task and click the Properties button to reveal the Task Properties Window.

Step 2:
Rename the task to "Verify Network".  Then click the "Edit Steps..." button.

Step 3:
The AutoMate Task Builder will appear.   This is where you specify exactly what the task is to do when it is started.  The Task Builder provides many functions to help you in creating and debugging your task.

When creating a task, its helpful to start with the "big picture" of what is required, and work down from there.  For our example, we need to ping a machine and keep track of the ping time.  If the ping time is 0, then we know that the machine is not responding, and we should write an entry to a log file to indicate this. Also, we would want to reboot that machine in an attempt to restore its network connectivity. So, our first step is to figure out how to ping the machine.  We can do this using AutoMate's Ping action.  For our purposes, the Ping Machine action will need to store the ping time.  The Ping Machine action can optionally use an AutoMate Variable to accomplish this.  Variables are used to store a value that may change over time, and they can be used by almost all AutoMate actions to exchange information from step to step.  Lets start building our task by making an AutoMate variable.  Find the "Create Variable" action in Available Actions by expanding the Variables group (either by clicking the + to the left of the word "Variables " or by double-clicking on "Variables ").  Double-click "Create Variable", or drag it to the right hand side by holding the left-mouse button over the action name, sliding the mouse over the steps section of the Task Builder, and releasing the mouse button.

Step 4:
Every AutoMate variable requires a name unique to the task.  The variable must start with a letter, and cannot contain special symbols or spaces.  We'll name our variable "PingTime".  We can also specify a default value (the value the variable will contain if it is used before it is set by a Set Variable action or another step), and a description.  We'll set the default value to 0, and the description as "Used to hold the time of our latest ping."  (The description is just for notational purposes in the Task Builder, and does not alter the use of the variable in any way.)  When you've filled out this information, click OK.

Step 5:
Our next step is to ping the machine.  Lets use a new way to quickly find the action we are looking for in the Step Builder.  In the search box in the Available Actions (the area marked "Search for action"), type "ping" and press return.  AutoMate will highlight the Ping Machine action for you.  This method can bse used to quickly locate and select an available action in the Step Builder.  Select the action.  The Ping Machine action requires a hostname or IP address to ping.  For our example, we'll use www.myserver.com.  Use the dropdown field of the "Populate variable with relay time" to select the PingTime variable.  (We can do this because we have already created the variable.  Any field in AutoMate that takes a variable as a parameter has a dropdown that contains all the variables already created.  You could, however, manually type in a variable name and create the variable later.)
 The action should look something like that image below.  Click OK.

Step 6:
For the next step, we need to determine whether or not the ping time (which will be stored in the variable "PingTime") is 0, and take appropriate action if it is.  One way on controlling the flow of the task is by using an IF action.  An IF action will test a condition, and if true, execute the block of steps between the IF step and either an ELSE action, or an END IF action, whichever comes first.  If the condition is false, AutoMate will execute the steps between the ELSE step and the ENDIF step (if an ELSE step is present).  Our case is simple because we only want to execute a set of steps if and only if PingTime is 0.  Therefore, an ELSE action isn't necessary.

Select the IF action under the Flow group, and drag it to the end of the task. Build your expression by typing "%PingTime%" (without the quotes) into the first field.  Surrounding the name of the variable with % tells AutoMate that you want to use the value of PingTime, and not the actual text that spells PingTime.  The next field should be IS, and the last field should be 0.  When read, your task will say "If %PingTime% is 0 then..."

Step 7:
Press OK.  Notice that AutoMate has automatically placed an End If step into the task for you.  Every IF step has to have a corresponding End If step, even if there is nothing inbetween them in your task.  

Step 8:
Now we need to build what will occur if the ping time is 0.  We wanted to write a value to a log file, and try to reboot the machine.  Locate the Write to File action in the File group, and drag it inbetween the IF and EndIf steps of your task.

Step 9:
For the file, we need to specify the name and path of our log file.  Click the open folder button, and browse to your My Documents folder.  For the filename, enter "PingLog.log".  Leave the method as "Append to end of file".  This will cause each successive log entry to be written to the end of our log file if it already exists, and create it from scratch if it does not.  

Next, we need to specify what we want written to the log.  We'd like to have the date and time that the ping failed, and write what we are going to do about it.  To do this, we'll need to build an expression.  Place the cursor in the "Date to write" field and click the "gear" button to the right.  This will open the Expression Builder.

Step 10:
The expression builder is an editor that aids in creating simple or complex AutoMate expressions.  And expression is a logical element that greatly increases the power of your steps.  For this example, we are going to use a very simple expression that returns the current date.  Along the lefthand side of the Expression Builder, locate the folder named "Functions" and select it.  The righthand side will reveal an alphabetical list of all of the standard VBA-compatible functions and properties available to you.  Find Now(), and double-click.  The word "Now()" will automatically appear in the Expression field of the Expression Builder.  Some functions require additional parameters, which you could enter into the expression field.  The Now() function, however, does not require any additional work.  Click the Insert button to place the expression you just created into the Write to File action.

NOTE: Anytime  you see the "gear" button next to a field,  you can use an expression in that field.  Click the gear to open the Expression Builder to create your expression.

Step 11:
Now that the expression is in our Date to write field, we can add the rest of the test we'd like to log.  Next to the final %, place a space and the text "Machine did not respond.  Attempting reboot."  Now, when the task runs, the expression will be replaced by the current date and time, and the text written to the log will be something like this:

12/02/2002 Machine did not respond. Attemping reboot.

Click OK to add this step and continue building your task.

Step 12:
The final step we need is a way to reboot the machine.  Locate the Restart Action under the Session group, and like before, drag it to the right hand side, just under the "Write to File" step.   Select "Restart a remote computer", and in the field labeled "Computer to restart", enter the name of the server you just pinged (in our example,  "www.myserver.com").  Click OK.  (Note that you will need to have sufficient rights on your Windows network to reboot the machine in this way.  Typically, only system administrators and power users are granted this kind of privilege.)

Step 13:
Thats it for the functionality!  Whenever the task executes, it will ping www.myserver.com, and if the ping response is 0, it will write a message to you log file in your My Documents directory, and attempt to reboot the machine.  Before we finish with the task though, lets beautify it a bit to make it a little more readable.  It is good practice to indent steps that appear inbetween IF/EndIf blocks and Loops, with each imbedded "block" progressively indented. We should indent the Write To File and Restart actions.  To do this, select step 4 and 5 by click each step while holding down the Ctrl key.  Next, while still holding the Ctrl key, press the right arrow.  The steps will indent one (you can also dedent by using Ctrl-Left Arrow).  Your task should now look something like this:

Step 14:
Click the Update and Close button.  Click the Triggers tab.  We'll add a Schedule Watcher so that the task will run every hour on the hour.  Click the Add button, select the Schedule Watcher, and click OK.

Step 15:
For interval, use the drop down to select "hours".  Ensure that the "Every" field is "1 hour(s)".  Below, click the Now button next to "Next launcg date/time".  This will cause the task to trigger immediately after you click the OK button, and reschedule for the next hour (it will trigger immediately because, once you click OK, the task will be late).

Step 16:
Click OK.  Thats it!  The task should fire, and reschedule for the next hour.  

 

Some variations of the task:
For
step 10, you can customize the format of the date and time.  When you use Now(), the date is returned in the format specified in regional settings.  You can force your own format by using the Format() function.  For example, Format (Now(), "yyyy-mm-dd") will cause the date to be written like this: 2002-12-02 for December 2, 2002

For step 12, you can force a machine to reboot immediately and without regard for running applications by selecting "Force the system to restart without notifying applications".  Use with extreme caution though: doing this will cause any unsaved data in open applications to be lost.

 

See Also:

AutoMate Task Builder

Using variables

Using expressions

Introduction to the Expression Builder

Create Variable

Set Variable

If Action

Write To File Action

Restart Action

Schedule Watcher Trigger