Aslı
Aslı

Reputation: 115

in javascript how to call powershell script

I have a powershell script and I run on powershell like :

.\download-packages-license.ps1

But I want to call the javascript file before these lines.

var json =fs.readFileSync('../../dev/licenses/AllLicenses.json', 'utf8');
var options = {compact: true, ignoreComment: true, spaces: 4};
var result = convert.json2xml(json, options);

I could not anything in stackoverflow except : How to run a powershell script from javascript? So pls help thanks

Upvotes: 3

Views: 36362

Answers (3)

Garric
Garric

Reputation: 724

JScript

Simple way.

Works great. Suitable for simple operations, but loses some data when receiving line feeds as a single \n instead of the expected \r\n. Split by \r\n misinterprets the array which leads to the need to reformat the array of strings. Basically, I just wrote this, so there may be other problems.

var codepage='windows-1251';/*US-Europe-1252 and Js file in that codepage*/
var toPStext='Hello.\nПроверка русских буковок.';
var shell=new ActiveXObject('WScript.Shell');
var std=shell.Exec("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -command \
    $OutputEncoding = [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('"+codepage+"'); \
    Write-Output '"+toPStext+"'");
var output = std.StdOut.ReadAll().split('\r\n');// split('\n') - leads to the loss of some data
if (output.length>0){WScript.echo(output)}
//var x=WScript.StdIn.ReadLine();   

Line by line.

Unfortunately, powershell does not accept external data as sequences of lines, unlike cmd.exe with /q /k options, which simplifies this code, but will turn around problems with multiline output code. Of course, if necessary, you can transfer the code as a base64 string to powershell

var codepage='windows-1251';/*US-Europe-1252 and Js file in that codepage*/
var toPStext='Hello.\nПроверка русских буковок.';
var shell=new ActiveXObject('WScript.Shell');
var output=[],errors=[],WshRunning=0,WshFinished=1,WshFailed=2,i=0,tryCount=0;
var std=shell.Exec("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -command \
$OutputEncoding = [Console]::outputEncoding = [System.Text.Encoding]::GetEncoding('"+codepage+"'); \
Write-Output '"+toPStext+"'");
do{
    if (std.Status==WshFailed){
        errors.push('String '+i+' data read error: \n   '+std.StdErr.ReadLine());
        tryCount++
    }
    else if(std.Status==WshRunning){
        output.push(std.StdOut.ReadLine());
        tryCount=0;
        WScript.Echo('Running ...')
    }
    else if(std.Status==WshFinished){
        var last=std.StdOut.ReadLine();
        if(last.length>0){output.push(last)};last=undefined;
        tryCount=21;
        WScript.Echo('Finished ...')
    }i++;
}while(tryCount<21);        
if (output.length>0){WScript.echo(output)}
if (errors.length>0){WScript.echo(errors)}
var x=WScript.StdIn.ReadLine();

Upvotes: 1

Ritesh Singh Rajput
Ritesh Singh Rajput

Reputation: 339

I think this will work for you -

var spawn = require("child_process").spawn;
spawn("powershell.exe",[".\download-packages-license.ps1"]);

Upvotes: 5

Amit Baranes
Amit Baranes

Reputation: 8122

You can work with : Node-Powershell

Code Snippet :

const Shell = require('node-powershell');

const ps = new Shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

ps.addCommand('echo node-powershell');
ps.invoke()
.then(output => {
  console.log(output);
})
.catch(err => {
  console.log(err);
});

Upvotes: 2

Related Questions