我已使用node-windows模块在service.js中创建“演示服务”。
Service.js
var Service = require('node-windows').Service
var os = require('os')
var svc = new Service({
name: 'Demo Service',
description: 'This service get metro app list.',
script: require('path').resolve(`C:\\Users\\${os.userInfo().username}\\index.js`)
})
svc.install()
演示服务执行index.js,其中我已使用node-powershell模块通过“ Get-AppxPackage |选择名称的powershell命令。
index.js:
const shell = require('node-powershell');
var sysInfo = {}
var systemInformation = function () {
let ps = new shell({
executionPolicy: 'Bypass',
noProfile: true
});
ps.addCommand('Get-AppxPackage | Select Name')
ps.invoke()
.then(installedMetroApps => {
sysInfo.installedMetroApps = installedMetroApps;
ps.dispose();
next(null, 1)
})
.catch(err => {
ps.dispose();
throw err;
});
}
systemInformation();
当我使用节点(即节点index.js)执行index.js时,按预期方式获得了installMetroApps列表,但是当我通过Windows中的服务运行演示服务时,installedMetroApps列表为空。