Skip to content

This module provides the ability to spawn child processes (similar to Node.js Child-Process).

License

Notifications You must be signed in to change notification settings

tuckn/WshChildProcess

Repository files navigation

WshChildProcess

This module provides the ability to spawn child processes (similar to Node.js Child-Process).

tuckn/Wsh series dependency

WshModeJs
└─ WshZLIB
 └─ WshNet
  └─ WshChildProcess - This repository
   └─ WshProcess
     └─ WshFileSystem
       └─ WshOS
         └─ WshPath
           └─ WshUtil
             └─ WshPolyfill

The upper layer module can use all the functions of the lower layer module.

Operating environment

Works on JScript in Windows.

Installation

(1) Create a directory of your WSH project.

D:\> mkdir MyWshProject
D:\> cd MyWshProject

(2) Download this ZIP and unzip or Use the following git command.

> git clone https://github.com/tuckn/WshChildProcess.git ./WshModules/WshChildProcess
or
> git submodule add https://github.com/tuckn/WshChildProcess.git ./WshModules/WshChildProcess

(3) Create your JScript (.js) file. For Example,

D:\MyWshProject\
├─ MyScript.js <- Your JScript code will be written in this.
└─ WshModules\
    └─ WshChildProcess\
        └─ dist\
          └─ bundle.js

I recommend JScript (.js) file encoding to be UTF-8 [BOM, CRLF].

(4) Create your WSF packaging scripts file (.wsf).

D:\MyWshProject\
├─ Run.wsf <- WSH entry file
├─ MyScript.js
└─ WshModules\
    └─ WshChildProcess\
        └─ dist\
          └─ bundle.js

And you should include .../dist/bundle.js into the WSF file. For Example, The content of the above Run.wsf is

<package>
  <job id = "run">
    <script language="JScript" src="./WshModules/WshChildProcess/dist/bundle.js"></script>
    <script language="JScript" src="./MyScript.js"></script>
  </job>
</package>

I recommend this WSH file (.wsf) encoding to be UTF-8 [BOM, CRLF].

Awesome! This WSH configuration allows you to use the following functions in JScript (.\MyScript.js).

Usage

Now your JScript (.\MyScript.js ) can use helper functions to handle processes. For example,

splitCommand

var splitCommand = Wsh.ChildProcess.splitCommand; // Shorthand

splitCommand('"C:\\My Apps\\test.exe"');
// Returns:
// { mainCmd: 'C:\\My Apps\\test.exe'
//   argsStr: '' }

splitCommand('"C:\\My Apps\\test.exe" -s "fileName"');
// Returns:
// { mainCmd: 'C:\\My Apps\\test.exe'
//   argsStr: '-s "fileName"' }

splitCommand('mklink /D "filePath2" filePath1');
// Returns:
// { mainCmd: 'mklink'
//   argsStr: '/D "filePath2" filePath1' }

exec

Use Case:

  • Run an application asynchronously.
  • Run a DOS command asynchronously.
  • Specify the command in one string.
  • Not require the processing results.
  • Control the window style.
var exec = Wsh.ChildProcess.exec; // Shorthand

// Asynchronously create the directory
exec('mkdir C:\\Tuckn\\test');
exec('mkdir C:\\My Apps\\test'); // NG
exec('mkdir "C:\\My Apps\\test"'); // OK

// Asynchronously create the symbolic-link in D:\Temp
exec('mklink D:\\Temp\\hoge-Symlink "C:\\My Foo\\hoge"', {
  runsAdmin: true
});

exec('ipconfig /all', {
  closes: false, // Not automatically close the execution result of DOS window
  winStyle: 'activeDef' // Default window style is hidden
})

execSync

Use Case:

  • Run an application synchronously.
  • Run a DOS command asynchronously.
  • Specify the command in one string.
  • Require the processing results.
  • Control the window style.
var execSync = Wsh.ChildProcess.execSync; // Shorthand

var retObj = execSync('dir /A:H /B "C:\\Users"');
console.dir(retObj);
// Outputs:
// { exitCode: 0,
//   error: false,
//   stdout: "All Users
// Default
// Default User
// desktop.ini",
//   stderr: "" }

var retObj = execSync('"C:\\Image Magick\\identify.exe" C:\\test.png');
console.dir(retObj);
// Outputs:
// { exitCode: 0,
//   error: false,
//   stdout: "C:\test.png PNG 1920x1160 1920x1160+0+0 8-bit sRGB 353763B 0.000u 0:00.002",
//   stderr: "" }

Tip: execSync can get StdOut of the administrator privileges process by using options, runsAdmin: true and shell: true.

execFile

Use Case:

  • Run an application asynchronously.
  • Specify the arguments as Array.
  • Control the application later with WshOS.typeExecObject or ProcessID.
  • Not required to control the window style.
var execFile = Wsh.ChildProcess.execFile; // Shorthand

// Asynchronously run. The arguments are escaped automatically.
execFile('net.exe',
  ['use', '\\\\CompName\\My Dir', 'mY&p@ss>_<', '/user:Tuckn']
);
// Converted this args to 'use "\\\\CompName\\My Dir" mY^&p@ss^>_^< /user:Tuckn'

// Asynchronously run Notepad with active window
var rtn = execFile('notepad.exe', ['D:\\memo.txt']);
// Get the process info
var sWbemObjSet = wmi.getProcess(rtn.ProcessID);
...
rtn.Terminate(); // Exit the GUI process

// To execute the DOS command, you need option shell: true.
execFile('mkdir', ['C:\\Tuckn\\test']); // Error
execFile('mkdir', ['C:\\Tuckn\\test'], { shell: true }); // OK!

execFileSync

Use Case: Run a CUI application synchronously and when you want to receive the exit code and the processing stdout.

Use Case:

  • Run an application synchronously.
  • Specify the arguments as Array.
  • Require the processing results.
  • Not required to control the window style.
var execFileSync = Wsh.ChildProcess.execFileSync; // Shorthand

var retObj = execFileSync('net.exe',
  ['use', '\\\\CompName\\IPC$', 'mY&p@ss>_<', '/user:Tuckn']
);
console.dir(retObj);
// Outputs:
// { extiCode: 0,
//   error: false,
//   stdout: "....",
//   stderr: "" }

// Run IrfanView with active window.
var retObj = execFileSync('7z.exe', ['u', '-tzip', 'my.zip', 'D:\\My data']);
// and this WSH process is stopping until you close the window.
if (retObj.exitCode === 0) { ... }

Option dry-run

No executes, returns the string of command.

var execFileSync = Wsh.ChildProcess.execFileSync; // Shorthand

var log = execFileSync('net.exe',
 ['use', '\\\\CompName\\IPC$', 'mY&p@ss>_<', '/user:Tuckn'],
 { isDryRun: true, shell: true }
);
console.log(log);
// Outputs:
// dry-run [os.exeSync]: C:\Windows\System32\cmd.exe /S /C"net.exe use \\CompName\IPC$ mY^&p@ss^>_^< /user:Tuckn 1> C:\%TMP%\stdout.log 2> C:\%TMP%\stderr.log"

Many other functions will be added. See the documentation for more details.

Dependency Modules

You can also use the following helper functions in your JScript (.\MyScript.js).

Documentation

See all specifications here and also below.

License

MIT

Copyright (c) 2020 Tuckn

About

This module provides the ability to spawn child processes (similar to Node.js Child-Process).

Resources

License

Stars

Watchers

Forks

Packages

No packages published