In this blog post I will walk you through the simple steps to create a self request form using PowerShell and NODE.JS. As you guessed, I used the below packages to make my job easy.
- Express
- node-powershell
- pug
Our goal is to deliver a form as illustrated below!
Looks simple and clean which is more than enough for our demo. Let’s focus the steps to back fill information from Active Directory. The field values like First Name , Last Name, Email and Department will be back filled from Active Directory!
Get Started
Navigate to your desired location create a project root folder and in our case we created as xSelfServiceRequestForm and below that create views and scripts folder where views holds index and RequestForm landing pages named index.pug and RequestForm.pug respectively. Place the PowerShell script file GetUserInformation.ps1 under scripts folder. The structure appears as illustrated below
- xSelfServiceRequestForm
- views
- index.pug
- RequestForm.pug
- scripts
- GetUserInformation.ps1
- server.js
Install required packages
Now we are in the root folder i.e xSelfServiceRequestForm which is the correct location to start the project work.
PS C:\xSelfServiceRequestForm>npm init
Just follow the instructions and key in the required information which yields the package.json file in the root folder. Now, install all required packages in one using the below snippet
PS C:\xSelfServiceRequestForm>npm install express node-powershell pug -s
Which yields the package.json file as illustrated below.
{ "name": "xSelfServiceRequestForm", "version": "1.0.0", "description": "Demo Node Application", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node server.js" }, "author": "Chendrayan Venkatesan", "license": "ISC", "dependencies": { "body-parser": "^1.18.2", "express": "^4.16.3", "node-powershell": "^3.3.1", "pug": "^2.0.3" } }
Let’s code
Open server.js and use the below code
var express = require('express'), bodyparser = require('body-parser'), app = express(), shell = require('node-powershell'), ps = new shell({ executionPolicy: 'bypass', noProfile: true }), path = require('path'); app.set('views', path.join(__dirname, 'views')) app.set('view engine', 'pug'); app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json()) app.get('/', function (request, response) { response.render('index', { title: "Node PowerShell" }) }); app.get('/SelfServiceRequestForm', function (request, response) { ps.addCommand('./scripts/GetUserInformation.ps1') ps.invoke().then(output => { var result = JSON.parse(output) response.render('RequestForm', { title: "Welcome", firstname: result['GivenName'], surname: result['SurName'], displayname: result['DisplayName'], mail: "ChenV@contoso.com", }) }) }) app.listen(3000) console.log("Your application is running on http://localhost:3000")
PUG File
The RequestForm.pug code appears as shown below
doctype html head meta(charset='UTF-8') meta(name='viewport', content='width=device-width, initial-scale=1.0') meta(http-equiv='X-UA-Compatible', content='ie=edge') title Request Form p(align='right' style='color: blue') Welcome b(style='color: blue') #{displayname} h1(align='center') Employee Onboarding Form hr form table tr td(align='right') First Name: td(align='left') input(type='text', name='first', value=firstname , readonly='true') tr td(align='right') Last Name: td(align='left') input(type='text', name='last', value=surname, readonly='true') tr td(align='right') Email: td(align='left') input(type='text', name='email' , value=mail, readonly='true') tr td(align='right') Department: td(align='left') input(type='text', name='department' , value=Department) tr td(align='left') input(type='submit', name='Submit') td(align='right') input(type='reset', name='Clear') hr footer(align='center') Copyright © 2018 Chendrayan Venkatesan
PowerShell Code
It’s your choice to use AD module or ADSI module. But, I used a snippet as shown below
$adsi = [adsisearcher]::new() $adsi.Filter = "(&(ObjectCategory=User)(samaccountname=$env:username))" $results = $adsi.FindOne().Properties $adsi.dispose() [pscustomobject]@{ GivenName = $results['givenname'] -as [string] SurName = $results['sn'] -as [string] DisplayName = $results['displayname'] -as [string] } | ConvertTo-Json -Compress
Output
Welcome Chendrayan Venkatesan
Employee Onboarding Form
Cool! It’s just a beginning and in my next blog I will explain the POST method in detail until then enjoy PowerShell. Follow me on twitter @ChendrayanV!