3

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

3 Answers 3

5

I think this will work for you -

var spawn = require("child_process").spawn;
spawn("powershell.exe",[".\download-packages-license.ps1"]);
Sign up to request clarification or add additional context in comments.

2 Comments

@Rithest Singh Rajput it is my fault.The code run but powershell script is not worked . My code create a new txt file . Created file is empty. I checked again when i run on command line txt is not empty.
It worked for powershell you are right @Ritesh Singh Rajput
2

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);
});

3 Comments

example call only comment how i call my download-packages-license.ps1 @Amit Baranes
I wrote const ps = new Shell({ executionPolicy: 'Bypass', noProfile: true }); ps.addCommand('./download-packages-license.ps1'); but terminal work forever @Amit Baranes
What are you trying to do ?
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.