WshOS

Adds useful functions that handle Windows OS into WSH (Windows Script Host).

tuckn/Wsh series dependency

WshModeJs
└─ WshZLIB
 └─ WshNet
  └─ WshChildProcess
   └─ WshProcess
     └─ WshFileSystem
       └─ WshOS - This repository
         └─ 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/WshOS.git ./WshModules/WshOS
or
> git submodule add https://github.com/tuckn/WshOS.git ./WshModules/WshOS

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

D:\MyWshProject\
├─ MyScript.js <- Your JScript code will be written in this.
└─ WshModules\
    └─ WshOS\
        └─ 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\
    └─ WshOS\
        └─ 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/WshOS/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 paths.
For example,

var os = Wsh.OS; // Shorthand

os.is64arch(); // true
os.tmpdir(); // 'C:\\Users\\YourUserName\\AppData\\Local\\Temp'
os.makeTmpPath(); // 'C:\\Users\\UserName\\AppData\\Local\\Temp\\rad6E884.tmp'
os.homedir(); // 'C:\\Users\\%UserName%'
os.hostname(); // 'MYPC0123'
os.cmdCodeset(); // 'shift_jis'

os.writeLogEvent.error('Error Log');
// Logs the error event in Windows Event Log.

os.surroundCmdArg('C:\\Program Files');
// Returns: '"C:\\Program Files"'
os.surroundCmdArg('C:\\Windows\\System32\\notepad.exe');
// Returns: 'C:\\Windows\\System32\\notepad.exe'

os.escapeForCmd('/RegExp="^(A|The) $"');
// Returns: '"/RegExp=\\"^^(A^|The) $\\""'

// Executer1: Asynchronously run.
os.shRun('notepad.exe', ['D:\\Test.txt'], { winStyle: 'activeMax' });
// You can control a window style. but can not receive a return value.

// Executer2: Synchronously run.
os.shRunSync('notepad.exe', ['D:\\Test.txt'], { winStyle: 'activeMin' });
// You can receive a return number but can not receive StdOut.

// Executer3: Not wait for finished the process
var oExec1 = os.shExec('dir', 'D:\\Temp1', { shell: true });
// You can control the application later.
while (oExec1.Status == 0) WScript.Sleep(300); // Waiting the finished
var result = oExec2.StdOut.ReadAll();
console.log(result); // Outputs the result of dir

// Executer4: Wait for finished the process
var retObj2 = os.shExecSync('ping.exe', ['127.0.0.1']);
console.dir(retObj2);
// You can receive a return number, command and, also StdOut.
// Outputs: {
//   exitCode: 0,
//   command: "ping.exe 127.0.0.1",
//   stdout: <The result of ping 127.0.0.1>,
//   stderr: ""
//   error: false };

os.isAdmin(); // false
os.runAsAdmin('mklink', 'D:\\Temp-Symlink D:\\Temp', { shell: true });

os.Task.create('MyTask', 'wscript.exe', '//job:run my-task.wsf');
os.Task.exists('MyTask'); // true
os.Task.xmlString('MyTask'); // Returns: The task XML string
os.Task.run('MyTask');
os.Task.del('MyTask');

os.assignDriveLetter('\\\\MyNAS\\MultiMedia', 'M', 'myname', 'mY-p@ss');
// Return 'M'

var pIDs = os.getProcessIDs('Chrome.exe');
// Returns: [33221, 22044, 43113, 42292, 17412]
var pIDs = os.getProcessIDs('C:\\Program Files\\Git\\bin\\git.exe');
// Returns: [1732, 4316]

os.addUser('MyUserName', 'mY-P@ss');
os.attachAdminAuthorityToUser('MyUserName');
os.deleteUser('MyUserName');

// and so on...

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