Node JSPowerShellPSHTML

Experiment – Using PSHTML in node.js and host a Azure Web App

Is it possible to use PSHTML in node.js and host a site in Azure Web App? Yes, it is possible. Just a small tweak is needed and that is to upload the PSHTML module in project root folder. So, in Azure Web App the root folder path is like illustrated below

D:\home\site\WWWROOT\PSHTML\PSHTML\PSHTML.psd1

Here – PSHTML is our module to write HTML in PowerShell way!

Now, that I cleared the ground let us move on to the demo.

PSHTML.js

var express = require('express');
var router = express.Router();
var spawn = require('child_process').spawn;

router.use('/', function (req, res) {
    res.header('Content-Type', 'text/html;charset=utf-8');
    var child = spawn('powershell.exe',
        ["D:\\home\\site\\WWWROOT\\scripts\\pshtml.ps1"]);
    child.stdout.on('data', function (data) {
        res.send(data)
    })
});

module.exports = router;

PSHTML.ps1

# Import-Module .\iAutomate\PSHTML\PSHTML\PSHTML.psd1
Import-Module "D:\home\site\wwwroot\PSHTML\PSHTML\PSHTML.psd1"
html -Content {
    head -Content {
        Title "iAutomate | PSHTML"
        Link -href "https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" -rel "stylesheet" -Integrity "sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" -CrossOrigin "anonymous"
        script -src "https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" -integrity "sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" -crossorigin "anonymous"
        script -src "https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.bundle.min.js" -integrity "sha384-zDnhMsjVZfS3hiP7oCBRmfjkQC4fzxVxFhBx8Hkz2aZX8gEvA/jsP3eXRCvzTofP" -crossorigin "anonymous"
    }
    body -Content {
        Div -Class "jumbotron" -Content {
            h1 -Class "display1" -Content "PSHTML Demo"
            hr
            p -Content "PSTHML ♥ PowerShell ♥ Azure Web App!"
        }
    }
}

Do you have questions like why not azure web jobs? why not pug? what not direct html? Feel free to connect with me and I am open to answer all your queries.

Enjoy PowerShell!

Leave a Reply

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