Task

.OS. Task

Source:

The wrapper object for functions to handle Windows TaskScheduler.

Methods

(static) create(taskName, cmdStr, argsopt, optionsopt) → {void|string}

Source:

Creates the scheduled task. To see API run "C:>SchTasks.exe /Create /?" or Schtasks create.

Example
Wsh.OS.Task.create('MyTask', 'wscript.exe', '//job:run my-task.wsf');
Parameters:
Name Type Attributes Description
taskName string

The Task name to create.

cmdStr string

The command to execute.

args Array.<string> | string <optional>

The arguments for the command.

options object <optional>

Optional parameters. See typeConvToCommandOptions.

Properties
Name Type Attributes Default Description
runsWithHighest boolean <optional>
false

Run as Admin

Throws:
    • If an error occurs during command execution, or if the command exits with a value other than 0.
    Type
    string
    • If an error occurs during command execution, or if the command exits with a value other than 0.
    Type
    string
Returns:
  • If isDryRun is true, returns string.
Type
void | string

(static) del(taskName, optionsopt) → {void|string}

Source:

Deletes the scheduled task. To see API run "C:>SchTasks.exe /Delete /?" or Schtasks. I wanted to name "delete" this method, but on JScript, can not use .delete as Object property name.

Example
Wsh.OS.Task.del('MyTask');
Parameters:
Name Type Attributes Description
taskName string

The Task name.

options object <optional>

Optional Parameters.

Properties
Name Type Attributes Default Description
isDryRun boolean <optional>
false

No execute, returns the string of command.

Throws:
  • If an error occurs during command execution, or if the command exits with a value other than 0.
Type
string
Returns:
  • If isDryRun is true, returns string.
Type
void | string

(static) ensureToCreate(taskName, mainCmd, argsopt, optionsopt, msecTimeOutopt) → {void|string}

Source:

Creates the scheduled task. If the task is already existing, delete this. and If the creation fails, it will retry to create for the specified number of seconds.

Example
var createTask = Wsh.OS.Task.ensureToCreate; // Shorthand

createTask('MyTask', 'wscript.exe', '//job:run my-task.wsf', 5000);
Parameters:
Name Type Attributes Default Description
taskName string

The task name to create.

mainCmd string

The executable file path or The command of Command-Prompt.

args Array.<string> | string <optional>

The arguments for the command.

options typeShRunOptions <optional>

Optional Parameters.

msecTimeOut number <optional>
10000

default: 10sec.

options.isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • If isDryRun is true, returns string.
Type
void | string

(static) ensureToDelete(taskName, msecTimeOutopt, optionsopt) → {void|string}

Source:

Deletes the scheduled task. If the deletion fails, it will retry to delete for the specified number of seconds.

Example
Wsh.OS.Task.ensureToDelete('MyTask', 5000); // Retry for 5sec
Parameters:
Name Type Attributes Default Description
taskName string

The task name.

msecTimeOut number <optional>
10000

default: 10sec.

options object <optional>

Optional Parameters.

Properties
Name Type Attributes Default Description
isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • If isDryRun is true, returns string.
Type
void | string

(static) exists(taskName, optionsopt) → {boolean|string}

Source:

Checks the scheduled task existing. To see API run "C:>SchTasks.exe /Query /?" or Schtasks.

Example
Wsh.OS.Task.exists('MyTask'); // Returns: true
Parameters:
Name Type Attributes Description
taskName string

The Task name.

options object <optional>

Optional Parameters.

Properties
Name Type Attributes Default Description
isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • If the task is existing returns true. If isDryRun is true, returns string.
Type
boolean | string

(static) run(taskName, optionsopt) → {void|string}

Source:

Runs the scheduled task. To see API run C:\>SchTasks.exe /Run /? or Schtasks.

Example
Wsh.OS.Task.run('MyTask');
Parameters:
Name Type Attributes Description
taskName string

The Task name

options object <optional>

Optional Parameters.

Properties
Name Type Attributes Default Description
isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • If isDryRun is true, returns string.
Type
void | string

(static) runTemporary(cmdStr, argsopt, optionsopt) → {void}

Source:

Create a task for the command, execute it, and then delete it. The advantage is that the command is always executed with Medium-WIL authority.

Example
var runOnce = Wsh.OS.Task.runTemporary; // Shorthand

runOnce('wscript.exe', '//job:run my-task.wsf');
Parameters:
https://tuckn.net/docs
Name Type Attributes Description
cmdStr string

The executable file path or The command of Command-Prompt.

args Array.<string> | string <optional>

The arguments for the command.

options object <optional>

Optional parameters. See OS.Task.create.options.

Returns:
Type
void

(static) xmlString(taskName, optionsopt) → {boolean|string}

Source:

Gets XML code of the scheduled task. To see API run "C:>SchTasks.exe /Query /?" or Schtasks.

Example
Wsh.OS.Task.xmlString('MyTask');
// Returns: XML code
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2022-01-01T01:01:01</Date>
    <Author>COMPUTER_NAME\UserName</Author>
    <URI>\TaskName</URI>
  </RegistrationInfo>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-1-11-1111111111-2222222222-3333333333-4444</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
  </Settings>
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2022-01-01T01:01:01</StartBoundary>
    </TimeTrigger>
  </Triggers>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Windows\System32\wscript.exe</Command>
      <Arguments>//job:autoQuit1 "C:\test\ModeJs\WshOS\assets\MockGUI.wsf"</Arguments>
    </Exec>
  </Actions>
</Task>
Parameters:
Name Type Attributes Description
taskName string

The Task name.

options object <optional>

Optional Parameters.

Properties
Name Type Attributes Default Description
isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • If the task is existing returns true. If isDryRun is true, returns string.
Type
boolean | string