Listing 1: RunMgr.js

/*  
   Uses the Penton.RegObject object to manage the entries in the Run registry
   key on the local or a remote computer.

   Using this script, you can list the entries in the Run key (/list), check
   if a value exists (/exists), add a value (/add), and delete a value
   (/delete).
*/

var SCRIPT_NAME = "RunMgr.js",
    REG_SUBKEY  = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

// Adds the Exists() method to the Array object:
// Returns true if the specified item exists as an array element
Array.prototype.Exists = function(item)
{
  var i, exists = false;

  for (i = 0; i < this.length; i++) {
    exists = item == this[i];
    if (exists) break;
  }

  return exists;
}

// Adds the Trim() method to the String object:
// Returns a copy of the string with leading and trailing spaces removed
String.prototype.Trim = function()
{
  return this.replace(/(^\s*)|(\s*$)/g, "");
}

WScript.Quit(main());

// Displays the script's usage message.
function usage()
{
  WScript.Echo("Manages the values in the Run registry key.\n" +
    "\n" +
    "Usage:\t" + SCRIPT_NAME + " [<computername>] /list\n" +
    "or:\t" + SCRIPT_NAME + " [<computername>] /exists:<name>\n"  +
    "or:\t" + SCRIPT_NAME + " [<computername>] /add:<name>=<value> | /delete:<name>\n" +
    "\n" +
    "<computername> is the name of a remote computer. If not specified, connect to\n" +
    "the local computer.\n" +
    "\n" +
    "/list lists the entries in the Run registry key.\n" +
    "\n" +
    "/exists checks for the existence of a Run item. The exit code will be 1 if it\n" +
    "exists, or 0 if it does not.\n" +
    "\n" +
    "/add adds the specified value to the registry.\n" +
    "\n" +
    "/delete deletes the specified value from the registry.");

  WScript.Quit(0);
}

// Exits script with an error message and exit code.
function die(message, exitcode)
{
  WScript.Echo(message);
  WScript.Quit(exitcode);
}

// Returns the script host executing the current script, in lowercase
// (usually cscript.exe or wscript.exe).
function scripthost()
{
  return WScript.FullName.substr(WScript.Path.length + 1).toLowerCase();
}

// Returns hex string equivalent to the specified number; if it's less
// than zero, add 0x100000000 to return a positive number. Equivalent to
// VBScript's Hex() function.
function hex(n)
{
  if (n >= 0)
    return n.toString(0x10).toUpperCase();
  else
    return (n + 0x100000000).toString(0x10).toUpperCase();
}

// Returns a nicely formed computer name (uppercase, without leading \\).
function fixcomputername(computername)
{
  // Remove leading and trailing spaces.
  computername = computername.Trim();

  // Remove leading backslashes.
  if (computername.substring(0, 2) == "\\\\")
    computername = computername.substring(2, computername.length);

  return computername.toUpperCase();
}

// For string s that contains delim, returns the string to the left of
// delim. If delim doesn't exist in s, return an empty string. The script
// uses this function to obtain the registry value name to the left of the
// = symbol when using /add.
function leftofdelim(s, delim)
{
  var i, result = "";

  s = s.Trim();
  i = s.indexOf(delim);
  if (i > 0) result = s.substring(0, i);

  return result;
}

// For string s that contains delim, return [Oops! Didn't follow my own rules here...] the string to 
the right of
// delim. If delim doesn't exist in s, return an empty string. The script
// uses this function to obtain the registry value data to the right of the
// = symbol when using /add.
function rightofdelim(s, delim)
{
  var i, result = "";

  s = s.Trim();
  i = s.indexOf(delim);
  if (i > 0) result = s.substring(i + 1, s.length);

  return result;
}

function main()
{
  var allargs, namedargs, computername;
  var validargs, arg, valid, result;
  var valuelist, valuename;

  allargs = WScript.Arguments;
  namedargs = allargs.Named;

  // No named command-line arguments, or /? specified.
  if ((namedargs.Length == 0) || (namedargs.Exists("?")))
    usage();

  // Retrieve computer name (first unnamed argument).
  if (allargs.Unnamed.Length > 0)
    computername = fixcomputername(allargs.Unnamed(0));

  // If computer name not specified, connect to local computer.
  if ((computername == null) || (computername == ""))
    computername = ".";

  // Script requires CScript host.
  if (scripthost() != "cscript.exe")
    die("You must run this script with the CScript host.", 1);

  // BEGIN CALLOUT A
  // Construct an array containing valid command-line arguments.
  validargs = new Array("list", "exists", "add", "delete");

  // Enumerate the namedargs collection.
  args = new Enumerator(namedargs);

  // Check all command-line parameters before attempting to connect
  // to a machine.
  for (; ! args.atEnd(); args.moveNext()) {
    arg = args.item().toLowerCase();
    valid = validargs.Exists(arg);
    if (valid)
      valid = arg == "list";
    if (valid) break;         // /list doesn't require an argument
    valid = (namedargs(arg) != null) && (namedargs(arg) != "");
    if (valid) {              // all other arguments require an argument
      if (arg == "add")       // add requires name=value
        valid = (leftofdelim(namedargs(arg), "=") != "") &&
          (rightofdelim(namedargs(arg), "=") != "");
      if (valid) break;
    }
  }

  if (! valid)
    die("Invalid command-line parameters. Use /? for help.", 1);
  // END CALLOUT A

  // BEGIN CALLOUT B
  regobj = new ActiveXObject("Penton.RegObject");

  // connect to computer
  result = regobj.Connect(computername);

  if (result != 0)
    die("Error 0x" + hex(result) + " connecting to " + computername, result);
  // END CALLOUT B

  // BEGIN CALLOUT C
  switch (arg) {
    case "list": {
      result = regobj.EnumValuesAndData("HKLM", REG_SUBKEY);
      if (result == 0) {
        // Copy the dictionary object to a JScript associative array
        valuelist = regobj.dictToJSArray(regobj.EnumDict);
        // Iterate the array and list its value/data pairs
        for (valuename in valuelist)
          WScript.Echo(valuename + "=" + valuelist[valuename]);
      }
      break;
    }
    case "exists": {
      result = regobj.ExistValue("HKLM",
                                 REG_SUBKEY,
                                 namedargs(arg));
      // Returns 1 if found, 0 if not found (use JScript's ternary operator)
      result = result ? result = 1 : result = 0;
      break;
    }
    case "delete": {
      result = regobj.DeleteValue("HKLM",
                                  REG_SUBKEY,
                                  namedargs(arg));
      break;
    }
    case "add": {
      // Replace ' with " characters; this lets us write a quoted
      // string to the registry.
      valuename = namedargs(arg).replace(/'/g, "\"");
      result = regobj.WriteValue("HKLM",
                                 REG_SUBKEY,
                                 leftofdelim(valuename, "="),
                                 "REG_EXPAND_SZ",
                                 rightofdelim(valuename, "="));
      break;
    }
  }
  // END CALLOUT C

  if (namedargs.Exists("v"))
    WScript.Echo("Exit code: " + result.toString() + " (0x" + hex(result) + ")");

  return result;
}