- Source:
This module provides the ability to spawn child processes (similar to Node.js Child Process).
Requires
Methods
(static) exec(command, optionsopt) → {0|void|string}
- Source:
Asynchronously executes the command within CommandPrompt. Similar to Node.js child_process.exec(). The function to be executed is one of os.shRun, os.runAsAdmin and os.Task.runTemporary.
Example
// Use Case: DOS commands or CUI applications that do not require processing results
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 });
// Note: Asynchronous behavior
var fs = Wsh.FileSystem;
fs.existsSync('C:\\Tuckn\\test'); // Returns: false
exec('mkdir C:\\Tuckn\\test'); // Asynchronously create the directory
fs.existsSync('C:\\Tuckn\\test'); // Returns: indefinite (true or false)
// Deprecations: Using GUI applications
exec('notepad.exe');
// Asynchronously run Notepad with hidden window... :-(
exec('notepad.exe', { winStyle: 'activeDef' });
// Asynchronously run Notepad with active window! :-)
// But it is easier to understand using `execFile('notepad.exe')`
// dry-run: No execute, returns the string of command.
var log = exec('mkdir C:\\Tuckn\\test', { isDryRun: true });
console.log(log);
// Outputs:
// dry-run [_shRun]: C:\Windows\System32\cmd.exe /S /C"mkdir C:\Tuckn\test"
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
command |
string | The executable file path or the command of CMD. |
|||||||||
options |
object |
<optional> |
See typeShRunOptions. Properties
|
Returns:
- A return value varies depending on an options parameter. options.runsAdmin: true or false and WSH process is admin => undefined. options.isDryRun: true => string. When others, returns 0.
- Type
- 0 | void | string
(static) execFile(file, argsopt, optionsopt) → {typeExecObject|number|void|string}
- Source:
Asynchronously executes the executable file. Similar to Node.js child_process.execFile().
Example
// Use Case: Applications that do not require processing results
var execFile = Wsh.ChildProcess.execFile; // Shorthand
// 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
// Arguments will be parsed
// 'mY& p@ss>_<' to '"mY^& p@ss^>_^<"'
execFile('net.exe',
['use', '\\\\CompName\\IPC$', 'mY& p@ss>_<', '/user:Tuckn']
);
// To execute the DOS command, you need option shell: true.
execFile('mkdir', ['C:\\Tuckn\\test']); // Error
execFile('mkdir', ['C:\\Tuckn\\test'], { shell: true }); // OK!
// dry-run: No execute, returns the string of command.
var log = execFile('notepad.exe', ['D:\\memo.txt'], { isDryRun: true });
console.log(log);
// Outputs:
// dry-run [_shRun]: notepad.exe D:\memo.txt
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
file |
string | The executable file path or the command of CMD. |
|||||||||||||||||||||
args |
Array.<string> | string |
<optional> |
The arguments. |
||||||||||||||||||||
options |
typeOsExecOptions |
<optional> |
See typeOsExecOptions. Properties
|
Returns:
- A return value varies depending on an options parameter. options.runsAdmin: true => void. options.runsAdmin: false and WSH process is admin => number. options.isDryRun: true => string. When others, returns typeExecObject.
- Type
- typeExecObject | number | void | string
(static) execFileSync(file, argsopt, optionsopt) → {typeExecSyncReturn|void|string}
- Source:
Executes the executable file. Similar to Node.js child_process.
Example
// Use Case: Applications that require processing results
var execFileSync = Wsh.ChildProcess.execFileSync; // Shorthand
var retObj = execFileSync('net.exe',
['use', '\\\\CompName\\IPC$', 'mY& p@ss>_<', '/user:Tuckn']
);
console.dir(retObj);
// Outputs:
// { exitCode: 0,
// error: false,
// stdout: "....",
// stderr: "" }
execFileSync('C:\\Program Files\\IrfanView\\i_view64.exe', 'C:\\result.png');
// Runs IrfanView with active window.
// and this JS process is stopping until you close the window.
console.log('Closed the window of IrfanView');
// dry-run: No execute, returns the string of command.
var log = execFileSync('net.exe',
['use', '\\\\CompName\\IPC$', 'mY& p@ss>_<', '/user:Tuckn'],
{ shell: true, isDryRun: 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"
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
file |
string | The executable file path or the command of CMD. |
|||||||||
args |
Array.<string> | string |
<optional> |
The arguments. |
||||||||
options |
typeOsExecOptions |
<optional> |
See typeOsExecOptions. Properties
|
Returns:
- A return value varies depending on an options parameter. options.runsAdmin: true or false and WSH process is admin => returns undefined. options.isDryRun: true => string. When others, returns typeExecSyncReturn.
- Type
- typeExecSyncReturn | void | string
(static) execFileViaJSON(jsonPath, optionsopt) → {void}
- Source:
[Experimental] Runs Wsh.ChildProcess.execFileSync according to the contents of the JSON file and write the result to that JSON file.
Example
var execFileViaJSON = Wsh.ChildProcess.execFileViaJSON; // Shorthand
// D:\sample.json
// { "file": "C:\\Program Files\\Image Magick\\identify.exe",
// "args": ["-verbose", "D:\\test.png"],
// "options": { "winStyle": "hidden" } }
execFileViaJSON('D:\\sample.json');
var fse = Wsh.FileSystemExtra;
var assoc = fse.readJsonSync('D:\\sample.json');
console.dir(assoc);
// { "file": "C:\\Program Files\\Image Magick\\identify.exe",
// "args": ["-verbose", "D:\\test.png"],
// "options": { "winStyle": "hidden" },
// "error": false,
// "stdout": "....",
// "stderr": "" }
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
jsonPath |
string | The JSON filepath |
|
options |
object |
<optional> |
Returns:
- Type
- void
(static) execSync(command, optionsopt) → {typeExecSyncReturn|string}
- Source:
Executes the command within CommandPrompt and returns a StdOut. Similar to Node.js child_process.execSync().
Example
// Use Case: DOS commands or CUI applications that require processing results
var execSync = Wsh.ChildProcess.execSync; // Shorthand
var retObj = execSync('dir /A:H /B "C:\\Users"');
console.dir(retObj);
// Outputs:
// { 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:
// { error: false,
// stdout: "C:\test.png PNG 1920x1160 1920x1160+0+0 8-bit sRGB 353763B 0.000u 0:00.002",
// stderr: "" }
// Deprecations: Using GUI applications
execSync('notepad.exe');
// Notepad is running with a hidden window... :-(
// and this JS process is stopping until you close the window.
console.log('Closed the Notepad!');
// dry-run: No execute, returns the string of command.
var log = execSync('dir /A:H /B "C:\\Users"', { isDryRun: true });
console.log(log);
// Outputs:
// dry-run [_shRun]: C:\Windows\System32\cmd.exe /S /C"dir /A:H /B "C:\Users" 1> C:\%TMP%\stdout.log 2> C:\%TMP%\stderr.log"
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
command |
string | The executable file path or the command of CMD. |
|||||||||
options |
object |
<optional> |
See typeShRunOptions. Properties
|
Returns:
- Basically returns typeExecSyncReturn. But, option.runsAdmin: true or false and WSH process is admin => exitCode is always undefined. options.isDryRun: true => string.
- Type
- typeExecSyncReturn | string
(static) isRunningAsAdmin(processNameopt) → {boolean}
- Source:
[Experimental] Checks if the process is running as administrator authority.
Example
var isRunningAsAdmin = Wsh.ChildProcess.isRunningAsAdmin; // Shorthand
isRunningAsAdmin(1234); // Returns: false
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
processName |
number | string |
<optional> |
The PID or the process name or the full path. If empty to specify all processes. |
Returns:
- Type
- boolean
(static) registerTaskForExecutingHighWIL()
- Source:
[W.I.P] @todo Registers the command in Task Scheduler to execute with administrator authority.
(static) setExePath(setVar, req32, req64opt, exePathopt) → {string}
- Source:
[Experimental]
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
setVar |
string | The variable to set a path |
|
req32 |
string | The 32bit executable file path |
|
req64 |
string |
<optional> |
The 64bit executable file path |
exePath |
string |
<optional> |
Returns:
- Type
- string
(static) spawnSync(command, optionsopt) → {typeExecSyncReturn}
- Source:
[W.I.P] Executes the command within CommandPrompt. Similar to Node.js child_process.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
command |
string | The executable file path or the command of CMD. |
|||||||||||||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- Type
- typeExecSyncReturn
(static) splitCommand(command) → {typesplitCommandReturn}
- Source:
Split the command into main part and arguments.
Example
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' }
Parameters:
| Name | Type | Description |
|---|---|---|
command |
string | The command to split. |
Returns:
- { mainCmd, argsStr }
(static) writeProcessPropsToJson(processNameopt, jsonPath) → {void}
- Source:
[Experimental] Writes information of the process as a JSON file.
Example
var writePInfo = Wsh.ChildProcess.writeProcessPropsToJson; // Shorthand
writePInfo('12345', 'D:\\process-info.json');
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
processName |
number | string |
<optional> |
The PID or the process name or the full path. If empty to specify all processes. |
jsonPath |
string | The JSON filepath to write. |
Returns:
- Type
- void