Nothing great here! Just a continuity of my previous blog post A Simple Self Request From using PowerShell and Node. So, in this blog post let me walk you through a simple steps to pass values from form to PowerShell using NODE.JS and to make this post easier below gif will help
Requirement – Receive Values from FORM and use it as PowerShell parameters! (Let’s receive two text parameters and calculate length)
Clear right? Let us move on to our actual exercise which is a demo to place an order – You plan for mongo, cosmos or SQL DB for your back end! In my case I didn’t use any 🙂
server.js
var express = require('express'), shell = require('node-powershell'), ps = new shell({ executionPolicy: 'bypass', noProfile: true }), bodyparser = require('body-parser'), path = require('path'), app = express(); app.use(bodyparser.urlencoded({ extended: true })); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); app.get('/', function (request, response) { response.render('index') }); /*app.post('/myPowerShellaction', function (request, response) { //response.send("Your First parameter: " + request.body.firstname) ps.addCommand("./scripts/myPSCode.ps1", [ { name: 'FirstName', value: request.body.firstname }, { name: 'LastName', value: request.body.lastname } ]), ps.invoke().then(output=>{ response.end(output) }) });*/ app.post('/myPowerShellaction1', function (request, response) { //response.send("Your First parameter: " + request.body.firstname) ps.addCommand("./scripts/myPSCode1.ps1", [ { name: 'FirstName', value: request.body.firstname }, { name: 'LastName', value: request.body.lastname }, { name: 'item', value: request.body.item } ]), ps.invoke().then(output => { //response.end("<b>Order Received From: + output</b>") var orders = JSON.parse(output); response.render('orderConfirmation', { results: orders }) }) }); app.listen(3000); console.log('I am running!')
PowerShell Code
param ( $FirstName, $LastName, $Item ) try { [pscustomobject]@{ FirstName = $FirstName LastName = $LastName Item = $Item OrderID = [guid]::NewGuid() } | ConvertTo-Json -Compress } catch { $_.Exception.Message }
index.pug
doctype html style. input[type=text], select { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } input[type=submit] { width: 100%; background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover { background-color: #45a049; } div { border-radius: 5px; background-color: #f2f2f2; padding: 20px; } h3 A simple Order Form div form(action='/myPowerShellaction1' method="post") label(for='fname') First Name input#fname(type='text', name='firstname', placeholder='Your name..') label(for='lname') Last Name input#lname(type='text', name='lastname', placeholder='Your last name..') label(for='item') Item select#item(name='item') option(value='pen') Pen option(value='paper') Paper option(value='catridge') Catridge input(type='submit', value='Submit')
orderConfirmation.pug
doctype html head style. #process { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%; } #process td, #process th { border: 1px solid #ddd; padding: 8px; } #process tr:nth-child(even) { background-color: #f2f2f2; } #process tr:hover { background-color: #ddd; } #process th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: olivedrab; color: white; } h1 { color: olivedrab; text-align: center } h1 We received your Order! table#process tbody tr th(style="text-align: center") FirstName th(style="text-align: center") LastName th(style="text-align: center") Item th(style="text-align: center") OrderID tr td=results.FirstName td=results.LastName td=results.Item td=results.OrderID
It’s time to spun our web server and enjoy the result !
Enjoy PowerShell and NODE.js!