Created
January 16, 2017 01:49
-
-
Save darrenjrobinson/571c765a23df3f42f7b9e3a2a5e66d74 to your computer and use it in GitHub Desktop.
PowershellUI-Index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
var PATH_TO_DECRYPT_UTILS_SCRIPT = "C:\\nodejs\\powershell-command-executor-ui-master\\decryptUtil.ps1"; | |
var PATH_TO_ENCRYPTED_CREDENTIALS_FILE = "C:\\nodejs\\powershell-command-executor-ui-master\\creds1.encrypted.credentials"; | |
var PATH_TO_ENCRYPTED_RPSCREDENTIALS_FILE = "C:\\nodejs\\powershell-command-executor-ui-master\\creds2.encrypted.credentials"; | |
var PATH_TO_SECRET_KEY = "C:\\nodejs\\powershell-command-executor-ui-master\\secret.key"; | |
var o365Utils = require('powershell-command-executor/o365Utils'); | |
var PSCommandService = require('powershell-command-executor'); | |
var StatefulProcessCommandProxy = require('stateful-process-command-proxy'); | |
var statefulProcessCommandProxy = new StatefulProcessCommandProxy({ | |
name: "StatefulProcessCommandProxy", | |
max: 1, | |
min: 1, | |
idleTimeoutMS: 120000, | |
logFunction: function(severity,origin,msg) { | |
console.log(severity.toUpperCase() + " " +origin+" "+ msg); | |
}, | |
processCommand: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', | |
processArgs: ['-Command','-'], | |
processRetainMaxCmdHistory : 20, | |
processInvalidateOnRegex : { | |
'any':[], | |
'stdout':[], | |
'stderr':[{'regex':'.*error.*'}] // anything comes in on stderr, w/ "error"... invalidate it | |
}, | |
processCwd : null, | |
processEnvMap : null, | |
processUid : null, | |
processGid : null, | |
initCommands: o365Utils.getO365PSInitCommands( | |
PATH_TO_DECRYPT_UTILS_SCRIPT, | |
PATH_TO_ENCRYPTED_CREDENTIALS_FILE, | |
PATH_TO_ENCRYPTED_RPSCREDENTIALS_FILE, | |
PATH_TO_SECRET_KEY, | |
10000,30000,3600000), | |
validateFunction: function(processProxy) { | |
return processProxy.isValid(); | |
}, | |
preDestroyCommands: o365Utils.getO365PSDestroyCommands(), | |
processCmdBlacklistRegex: o365Utils.getO365BlacklistedCommands(), | |
processCmdWhitelistRegex: o365Utils.getO365WhitelistedCommands(), | |
autoInvalidationConfig: o365Utils.getO365AutoInvalidationConfig(30000) | |
}); | |
var myLogFunction = function(severity,origin,message) { | |
console.log(severity.toUpperCase() + ' ' + origin + ' ' + message); | |
} | |
var psCommandService = new PSCommandService(statefulProcessCommandProxy, | |
o365Utils.o365CommandRegistry, | |
myLogFunction); | |
/* GET - console page */ | |
router.get('/', function(req, res, next) { | |
res.sendFile('console.html', { root: './public'}); | |
}); | |
// GET commands | |
router.get('/command/:commandName?', function(req, res, next) { | |
if (req.params.commandName) { | |
res.send(o365Utils.o365CommandRegistry[req.params.commandName]); | |
} else { | |
res.send(psCommandService.getAvailableCommands()); | |
} | |
}); | |
// GET command-service status | |
router.get('/command-service/status', function(req, res, next) { | |
var status = psCommandService.getStatus(); | |
res.send(status); | |
}); | |
// POST (execute command) | |
router.post('/command/execute/:commandName', function(req, res, next) { | |
psCommandService.execute(req.params.commandName,req.body) | |
.then(function(cmdResult) { | |
res.send(cmdResult); | |
}).catch(function(error) { | |
res.send({'error': { 'name': error.name, 'message': error.message, 'stack':error.stack}}); | |
}); | |
}); | |
// POST (generate command statement) | |
router.post('/command/generate/:commandName', function(req, res, next) { | |
var command = psCommandService.generateCommand(req.params.commandName,req.body); | |
res.send({'command': command}); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment