Node JSPowerShell

Build API and Web App using Node JS, PowerShell and PUG

I haven’t seen any new applications developed without REST API associated with it. Many vendors spend time to help developers who work on integrations. Lets see how we can use Node JS, PUG and PowerShell to share a web app and RESTful API

Our end-goal is to meet the below requirement

RESTful API

Web Application

Just below steps will help us to achieve the needs!

  1. Install node
  2. npm install express -s
  3. npm install pug -s
  4. npm install body-parser -s

PowerShell Script to query MYSQL

param(
    $employeeNumber
)
try {
    Import-Module 'C:\Projects\contoso\assemblies\MySql.Data.dll'
    $ConnectionString = "Server=localhost;Uid=root;Pwd=India890*;database=company;SslMode=none;"
    $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
    $Connection.ConnectionString = $ConnectionString
    $Connection.Open()
    $Command = New-Object MySql.Data.MySqlClient.MySqlCommand -ArgumentList (
        "select * from employee where employee_number='$employeeNumber'", $Connection
    )
    $DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter -ArgumentList ($Command)
    $DataSet = New-Object System.Data.DataSet
    [void]$DataAdapter.Fill($DataSet , "data")
    $DataSet.Tables['data'] |
        Select-Object employee_number , firstname , lastname , displayname , email | 
        ConvertTo-Json
}
catch {
    $_.Exception 
}

Note: You can use mysql package and achieve the same (For other infra needs I should or MUST use PowerShell and for a demo I picked mysql database)

Now here comes the node (employeeRecord.js) code

router.post('/employeeRecord', function (request, response) {
    child = spawn('powershell.exe',
        [ "C:\\Projects\\contoso\\scripts\\employeeRecordByID.ps1 -employeeNumber " +
            request.body.employeeNumber ])
    child.stdout.on('data', function (data) {
        var result = JSON.parse(data)
        //response.send(result)
        response.render('employeeRecord', {
            employee_number: result[ 'employee_number' ],
            firstname: result[ 'firstname' ],
            lastname: result[ 'lastname' ],
            displayname: result[ 'displayname' ],
            email: result[ 'email' ]
        })
    })
})

PUG (Intended HTML body)

    form(action='/Dummy', method='post')
        fieldset
            legend(style='text-align: center') Requester Information
            label(for='employeeNumber') Employee Number
            input#employeeNumber(type='text', name='employeeNumber', style='font-family: calibri', value=employee_number)
            label(for='firstName') First Name
            input#firstName(type='text', name='First Name', style='font-family: calibri',  value=firstname)
            label(for='lastName') Last Name
            input#lastName(type='text', name='Last Name', style='font-family: calibri',  value=lastname)
            label(for='displayName') Display Name
            input#displayName(type='text', name='Display Name', style='font-family: calibri',  value=displayname)
            label(for='email') Email
            input#email(type='text', name='Email', style='font-family: calibri',  value=email)
            input(type='submit', value='Submit')

For API – I created another route!

'use strict';
var express = require('express');
var router = express.Router();
var spawn = require('child_process').spawn, child;

router.post('/employeeRecord-api', function (request, response) {
    child = spawn('powershell.exe',
        [ "C:\\Projects\\contoso\\scripts\\employeeRecordByID.ps1 -employeeNumber " +
            request.body.employeeNumber ])
    child.stdout.on('data', function (data) {
        response.send(data)
    })
})

module.exports = router;

 

Output!

It’s easy! Enjoy PowerShell with Node JS

Leave a Reply

Your email address will not be published. Required fields are marked *