WMI

.OS. WMI

Source:

The wrapper object for functions to handle WMI (Windows Management Instrumentation. WBEM for Windows).

Methods

(static) execQuery(query, optionsopt) → {Array.<sWbemObjectSet>}

Source:

Executes the query to WMI (Windows Management Instrumentation. WBEM for Windows). Microsoft Docs Ref.1

Example
var wmi = Wsh.OS.WMI; // Shorthand

// Ex.1 Gets all processes
var query = 'SELECT * FROM Win32_Process';
var sWbemObjSets = wmi.execQuery(query);

// Ex.2 Add WHERE phrase (Do not omit the extension)
var query = 'SELECT * FROM Win32_Process WHERE Caption = "notepad.exe"';
var sWbemObjSets = wmi.execQuery(query);

sWbemObjSets.forEach(function (sWbemObjSet) {
  console.log('ProcessID: ' + sWbemObjSet.ProcessId);
  console.log('ExecutablePath: ' + sWbemObjSet.ExecutablePath);
  var iRetVal = sWbemObjSet.Terminate();
  console.log('Terminated it with returning value ' + iRetVal);
});
// Outputs:
//   ProcessID: 8356
//   ExecutablePath: C:\WINDOWS\system32\notepad.exe
//   Terminated it with returning value 0
//   ProcessID: 26712
//   ExecutablePath: C:\WINDOWS\system32\notepad.exe
//   Terminated it with returning value 0
//   ....

// The below code does not work. Because the return object is not JS objects.
// Use Wsh.OS.WMI.toJsObjects to convert to JS Objects
Object.keys(sWbemObjSets[0]).forEach(function (propName) {
  console.log(propName + ': ' + sWbemObjSets[0][propName]);
});
// No display

// Ex.3 Specifing options
var query = 'SELECT * FROM CIM_BIOSElement';
var sWbemObjSets = wmi.execQuery(query, {
  compName: 'otherComp.office.local',
  domainName: 'office.local'
});

// Require converting to read the array property in SWbemObjectSet
console.log(new VBArray(sWbemObjSets[0].BiosCharacteristics).toArray());

// Ex.4 Specifing the other namespace
var query = 'SELECT * FROM CIM_LogicalElement';
var sWbemObjSets = wmi.execQuery(query, {
  namespace: '\\\\.\\root\\Hardware'
});
Parameters:
Name Type Attributes Description
query string

The query to execute.

options typeExecWmiQueryOptions <optional>

Optional parameters.

Returns:
  • The array of enumerated SWbem-Object-Sets.
Type
Array.<sWbemObjectSet>

(static) getProcess(processNameopt, optionsopt) → {sWbemObjectSet|null}

Source:

Returns the SWbemObjectSet of the specified process

Example
var wmi = Wsh.OS.WMI;

var sWbemObjSet = wmi.getProcess('excel.exe');

console.log('ProcessID: ' + sWbemObjSet.ProcessId);
console.log('ParentProcessId: ' + sWbemObjSet.ParentProcessId);
var iRetVal = sWbemObjSet.Terminate();
console.log('Terminated it with returning value ' + iRetVal);
// Outputs:
//   ProcessID: 19220
//   ParentProcessId: 160624
//   Terminated it with returning value 0
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.

options typeGetProcessesOptions <optional>

Optional parameters.

Returns:
  • The enumerated SWbem-Object-Set.
Type
sWbemObjectSet | null

(static) getProcesses(processNameopt, optionsopt) → {Array.<sWbemObjectSet>}

Source:

Returns Enumerated SWbemObjectSets of the specified process. Microsoft Docs

Example
var wmi = Wsh.OS.WMI;

// Ex.1 Specifing a process name (Do not omit the extension)
var sWbemObjSets = wmi.getProcesses('chrome.exe');

sWbemObjSets.forEach(function (sWbemObjSet) {
  console.log('ProcessID: ' + sWbemObjSet.ProcessId);
  console.log('Handle: ' + sWbemObjSet.Handle);
  console.log('CreationDate: ' + sWbemObjSet.CreationDate);

  try {
    var iRetVal = sWbemObjSet.Terminate();
    console.log('Terminated it with returning value ' + iRetVal);
  } catch (e) {
    console.error('Failed to terminate. Already terminated?');
  }
});
// Outputs:
//   ProcessID: 8356
//   Handle: 18012
//   CreationDate: 20200217042344.517814+540
//   Terminated it with returning value 0
//   ....

// Ex.2 Specifing a process ID
var sWbemObjSets = wmi.getProcesses(8356);

// Ex.3 Specifing a full path
var sWbemObjSets = wmi.getProcesses('D:\\Apps\\Firefox\\firefox.exe');

// Ex.4 Specifing options
var sWbemObjSets = wmi.getProcesses('node.exe', {
  matchWords: ['nodemon'], // Compare with CommandLine
  excludingWords: ['80', '8080'] // Compare with CommandLine
});
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.

options typeGetProcessesOptions <optional>

Optional parameters.

Returns:
  • The array of enumerated SWbem-Object-Sets.
Type
Array.<sWbemObjectSet>

(static) getThisProcess() → {sWbemObjectSet}

Source:

Returns the enumerated SWbem-Object-Set of the self process.

Example
var wmi = Wsh.OS.WMI;

var thisProcess = wmi.getThisProcess();

console.log('ProcessID: ' + thisProcess.ProcessId);
console.log('Caption: ' + thisProcess.Caption);
console.log('Name: ' + thisProcess.Name);
console.log('CommandLine: ' + thisProcess.CommandLine);
console.log('ParentProcessId: ' + thisProcess.ParentProcessId);
// Outputs:
//   ProcessID: 23576
//   Caption: cscript.exe
//   Name: cscript.exe
//   CommandLine: cscript.exe  //nologo OS.test.wsf -t WMI_getThisProcess
//   ParentProcessId: 300
Returns:
  • The enumerated SWbem-Object-Set.
Type
sWbemObjectSet

(static) getWindowsUserAccounts() → {Array.<sWbemObjectSet>}

Source:

Returns Enumerated SWbemObjectSets of Windows user accounts. Microsoft Docs

Example
var sWbemObjSets = os.WMI.getWindowsUserAccounts();

sWbemObjSets.forEach(function (sWbemObjSet) {
  console.log('AccountType: ' + sWbemObjSet.AccountType);
  console.log('Caption: ' + sWbemObjSet.Caption);
  console.log('Name: ' + sWbemObjSet.Name);
  console.log('Domain: ' + sWbemObjSet.Domain);
  console.log('Status: ' + sWbemObjSet.Status);
});
// Outputs:
//   AccountType: 512
//   Caption: COMPNAME\Administrator
//   Name: Administrator
//   Domain: COMPNAME
//   Status: Degraded
//   AccountType: 512
//   Caption: COMPNAME\DefaultAccount
//   ....
Returns:
  • The array of enumerated SWbem-Object-Sets.
Type
Array.<sWbemObjectSet>

(static) getWithSWbemPath(sWbemPath, optionsopt) → {sWbemObjectSet|null}

Source:

Returns the Enumerated SWbem-Object-Sets from the SWbem path (e.g. 'Win32_LogicalDisk.DeviceID="C:"').

Example
var wmi = Wsh.OS.WMI;

var sWbemPath = 'Win32_LogicalDisk.DeviceID="C:"';
var sWbemObjSet = wmi.getWithSWbemPath(sWbemPath);

console.log('DeviceID: ' + sWbemObjSet.DeviceID);
console.log('Caption: ' + sWbemObjSet.Caption);
console.log('FileSystem: ' + sWbemObjSet.FileSystem);
console.log('FreeSpace: ' + sWbemObjSet.FreeSpace);
// Outputs:
//   DeviceID: C:
//   Caption: C:
//   FileSystem: NTFS
//   FreeSpace: 193899817343

// The below code does not work. Because the return object is not JS objects.
// Use Wsh.OS.WMI.toJsObject to convert to A JS Object
Object.keys(sWbemObjSet).forEach(function (propName) {
  console.log(propName + ': ' + sWbemObjSet[propName]);
});
// No display

// Ex.2 @todo Not work. Please tell me why
var sWbemPath = 'Win32_Process.ProcessId=11888';
var sWbemObjSet = os.WMI.getWithSWbemPath(sWbemPath); // Error [-2147217350]
Parameters:
Name Type Attributes Description
sWbemPath string

The SWbem path.

options typeExecWmiQueryOptions <optional>

Optional parameters.

Returns:
  • The enumerated SWbem-Object-Set.
Type
sWbemObjectSet | null

(static) toJsObject(sWbemObjSet) → {object}

Source:

Converts Enumerated SWbemObjectSets to JScript Object. Ref.2

Example
var wmi = Wsh.OS.WMI;

var sWbemObjSets = wmi.execQuery('SELECT * FROM CIM_BIOSElement');
var biosElements = wmi.toJsObject(sWbemObjSets[0]);
console.dir(biosElements.ListOfLanguages);
// Outputs: [
//   "en|US|iso8859-1",
//   "fr|FR|iso8859-1",
//   "zh|TW|unicode",
//   "zh|CN|unicode",
//   "ja|JP|unicode",
//   "de|DE|iso8859-1",
//   "es|ES|iso8859-1",
//   "ru|RU|iso8859-5",
//   "ko|KR|unicode" ],
Parameters:
Name Type Description
sWbemObjSet sWbemObjectSet

The SWbemObjectSet.

Returns:
  • The converted object.
Type
object

(static) toJsObjects(sWbemObjSets) → {Array.<object>}

Source:

Converts Enumerated SWbemObjectSets to JScript Objects. Ref.2

Example
var wmi = Wsh.OS.WMI;

var sWbemObjSets = wmi.execQuery('SELECT * FROM CIM_BIOSElement');
var wmiObjs = wmi.toJsObjects(sWbemObjSets);
console.dir(wmiObjs[0].ListOfLanguages);
// Outputs: [
//   "en|US|iso8859-1",
//   "fr|FR|iso8859-1",
//   "zh|TW|unicode",
//   "zh|CN|unicode",
//   "ja|JP|unicode",
//   "de|DE|iso8859-1",
//   "es|ES|iso8859-1",
//   "ru|RU|iso8859-5",
//   "ko|KR|unicode" ],
Parameters:
Name Type Description
sWbemObjSets Array.<sWbemObjectSet>

The SWbemObjectSets.

Returns:
  • The array of converted objects.
Type
Array.<object>