How to run PowerShell from node.js

by Doug Finke on February 26, 2013

in .Net,Microsoft,node.js,PowerShell

Tomasz Janczuk of Microsoft posted about owin, .NET and node.js. Here is the project on github.

The owin project allows hosting .NET 4.5 code in node.js applications running on Windows.

You need Windows x64 with node.js 0.8.x x64 installed (the module had been developed against node.js 0.8.19). You also need .NET Framework 4.5 on the machine.

PowerShell + node.js

Below is the PowerShell script at work.

The owin module allows running CPU-bound computations implemented in .NET in-process with the node.js application without blocking the node.js event loop.

image

The PowerShell script and C# implementation

The script has three steps:

  1. Compiles the C# which implements calling PowerShell as a Owinjs.Worker. This first step compiles to a DLL the C# implementing the PowerShell call simulating a CPU-bound computation. Simply, it calculates income by subtracting the cost from the revenue passed in from the JavaScript and does a Start-Sleep 5 to become CPU-bound.
  2. Next it takes the JavaScript, does a Set-Content to a file test.js.
  3. Finally, we invoke node on the test.js file created in step 2

Note: You also need the Owinjs.dll in the same directory as the script to make this happen. Download the zip file at the end of the post.

$ReferencedAssemblies = "$pwd\Owinjs.dll"            
            
$powerShell = @"

using System.Collections.Generic;
using System.Management.Automation;

namespace CalculateBudget
{
    public class Startup : Owinjs.Worker
    {
        protected override IDictionary Execute(IDictionary input)
        {
            var script = string.Format(@"{0}-{1} ; Start-Sleep 5", 
                input["revenue"], 
                input["cost"]);

            var income = PowerShell
                .Create()
                .AddScript(script)
                .Invoke()[0]
                .ToString();

            return new Dictionary { { "income", income } };
        }
    }
}
"@            
            
Add-Type -ReferencedAssemblies $ReferencedAssemblies `
    -OutputAssembly CalculateBudget.dll `
    -TypeDefinition $powerShell            
            
Write-Host -fore green "Compiled DLL"            
            
if($?) {            
            
@"
var owin = require('owin')

console.log('Starting long running operation...');
owin.worker(
    'CalculateBudget.dll',
    { revenue: 100, cost: 80 },
    function (error, result) {
        if (error) throw error;
        console.log('Result of long running operation: ', result);
    }
);

setInterval(function () {
    console.log('Node.js event loop is alive!')
}, 1000);
"@ |  Set-Content -Encoding Ascii .\test.js            
            
Write-Host -fore green "Create JavaScript file"            
Write-Host -fore green "Running nodejs"            
            
node .\test.js            
            
}

This opens interesting ways to experiment with Node, .NET and PowerShell.

Download

Here it is.       Owinjs.zip

Enjoy!

{ 1 trackback }

The Morning Brew - Chris Alcock » The Morning Brew #1304
02.27.13 at 4:32 am

{ 2 comments… read them below or add one }

Grant Steinfeld 02.28.13 at 8:41 am

Wow, now really one can become the polyglot master of ‘The Stack’

bitsofinfo 01.19.15 at 1:09 pm

This might also be helpful to folks who need to communicate with external processes on windows using node (i.e. powershell) https://www.npmjs.com/package/stateful-process-command-proxy

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post:

You are forbidden!