Skip navigation

New Tricks for Your Scripting Tool Belt

Downloads
48416.zip

My GroupedShutdowns script uses two techniques that you'll probably want to add to your scripting tool collection: multiple-usage routines and entry-point labels. Multiple-usage routines are a code-reuse technique, and entry-point labels provide a way to allow script flow to jump to a particular starting point.

Multiple-Usage Routines
In my Windows Scripting Solutions articles, I've often encouraged modularizing script code so that you can more easily leverage that code in future scripts. After your code is in production, you don't need to keep reinventing the wheel—just reuse the code, as I did with several code sections in the GroupedShutdowns script. While writing the script, I noticed that it ran a Ping routine three times and sent the Shutdown command twice. If I could just call the same Ping and Shutdown sections repeatedly, I could eliminate three-fifths of the script's code. Furthermore, if I needed to change code in the Ping or Shutdown modules, I could change it in just one module, rather than in several modules throughout the script.

The only disadvantage to using multiple-usage routines is that to do so, you need to write some additional code to make the script "track" which section of the script it's launched from. Callout A in the main article's Listing 1 shows the code that performs the call to the appropriate subroutine. Notice that I use an argument to call the repetitive routines. This argument (1, 2, or 3 in the Ping test and 1 or 2 in the Shutdown command code) helps track which usage of the code is in progress. Callout B provides an example of the called routine that does the Ping pretest, and callout C contains the code that tests whether a server has failed the Ping pretest and notifies the operator.

If you're writing a script that you think might benefit from reusing routines, I recommend you first write it the long way using the single-use routines, then debug your code. After the script is working correctly, you can try removing the extra repeated routines.

Entry-Point Labels
Occasionally in a script, you might need to code the capability to start at a point other than the beginning. You could do so either by opening the script and saving a newer shortened version, commenting out code, or using a temporarily added Goto statement to change the default script flow. However, a better way is to use what I call entry-point labels: coding a script to accept arguments that allow script flow to jump to a desired script starting point. To do so, you add labels between each routine so that you can jump into the script at whatever logical location you require.

In the GroupedShutdowns script, you can jump into the script at any of five different locations by entering an argument at the command line at runtime. Callout A shows the code that uses the argument to route the script flow to the proper label location. Alternatively, if you omit the argument, the script reverts to running all the modules. GroupedShutdown's entry-point labels essentially give you six scripts that perform these tasks:

  • Skip initial pre-Ping test of the nodes and go straight to shutting down Group 1 nodes.

  • Skip over pre-Ping test and shutdown of Group 1 nodes. Proceed to ping Group 1 nodes.

  • Skip over pre-Ping test, shutdown of Group 1 nodes, and Ping test on Group 1 nodes. Proceed to shut down Group 2 nodes.

  • Skip over pre-Ping test, shutdown of Group 1 nodes, Ping test on Group 1 nodes, and shutdown of Group 2 nodes. Proceed to Ping test of Group 2 nodes.

  • Skip over pre-Ping test, shutdown of Group 1 nodes, Ping test on Group 1 nodes, shutdown of

  • Group 2 nodes, and Ping test of Group 2 nodes. Proceed to the uptime test of all nodes.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish