Tuesday, 4 April 2017

node.js, (windows-service-controller) need service name for error returned

I am attempting to check if a number of Windows services are running so I can log errors if they are not. I am using sc.query from var sc = require('windows-service-controller');

If the service exists, I do get the state (running or not). And if the service does not exist, I do get an error, and I can trap that error. The error is generic in that it does not return the name of the service that I was attempting to report on. The error I am getting is:

[SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not exist as an installed service.

I believe due to the fact that node.js is asynchronous, the name of the service is no longer available at the time the reject/error function is called.

Below is the code:


function MyServiceMonitor() {

var test1 = "MpsSvc";    //Windows firewall
var test2 = "MSSQL$XYZ";
var SqlS = "SQLScheduler Service";
var SqlR = "ReportServer $SQLEXPRESS";
var srvcArray = new Array();
srvcArray = [SqlR, SqlS, test1, test2];  

/**** Windows Service Console..  */
var sc = require('windows-service-controller');

for (i = 0; i < srvcArray.length; i++) {
    srvcName = srvcArray[i];
    sc.query({ name: srvcName })
        .then(function (services) { console.log("services = ", services); }, function (error) { console.log("then error ", error);});   //error is caught but it does not have the name of service 
} 

}

MyServiceMonitor();


How can I pass the service name to the reject/error function?



via Pcm2830

No comments:

Post a Comment